bumpsoo 2024-12-14 20:42:14 +09:00
parent 511ce16e0f
commit bac9beaa68

View file

@ -0,0 +1,22 @@
# https://leetcode.com/problems/continuous-subarrays
from typing import List
class Solution:
def continuousSubarrays(self, nums: List[int]) -> int:
memo = {}
left = 0
right = 0
result = 0
while right < len(nums):
memo[nums[right]] = memo.get(nums[right], 0) + 1
while max(memo) - min(memo) > 2:
memo[nums[left]] -= 1
if memo[nums[left]] == 0:
del memo[nums[left]]
left += 1
right += 1
result += right - left
return result