LeetCode题解(0904):水果成篮(Python)
发布日期:2021-06-29 20:18:03 浏览次数:3 分类:技术文章

本文共 585 字,大约阅读时间需要 1 分钟。

题目:(中等)

标签:数组、双指针、滑动窗口

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( 1 ) O(1) O(1) 1160ms (17.70%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:    def totalFruit(self, tree: List[int]) -> int:        ans = 0        l, r = 0, 0        window = collections.Counter()        while r < len(tree):            window[tree[r]] += 1            while len(window) > 2:                window[tree[l]] -= 1                if window[tree[l]] == 0:                    window.pop(tree[l])                l += 1            ans = max(ans, r - l + 1)            r += 1        return ans

转载地址:https://dataartist.blog.csdn.net/article/details/112788512 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(0909):蛇梯棋(Python)
下一篇:LeetCode题解(0900):RLE迭代器(Python)

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月23日 19时50分22秒