This commit is contained in:
parent
511ce16e0f
commit
bac9beaa68
1 changed files with 22 additions and 0 deletions
22
medium/continuous_subarrays.py
Normal file
22
medium/continuous_subarrays.py
Normal 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
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue