Re: [閒聊] 每日leetcode
2762. Continuous Subarrays
## 思路
sliding window
用counter記錄window裡面的值跟個數
## Code
```python
class Solution:
def continuousSubarrays(self, nums: List[int]) -> int:
counter = defaultdict(int)
res = left = 0
for right, num in enumerate(nums):
counter[num] += 1
while max(counter) - min(counter) > 2:
if counter[nums[left]] == 1:
del counter[nums[left]]
else:
counter[nums[left]] -= 1
left += 1
res += right - left + 1
return res
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 94.156.205.35 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1734139760.A.D67.html
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1204 之 1554 篇):