Skip to content

Commit b262b08

Browse files
Merge pull request #14 from theforestvn88/august
2762
2 parents 82baf8a + 73b3bf4 commit b262b08

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# @param {Integer[]} nums
2+
# @return {Integer}
3+
def continuous_subarrays(nums)
4+
count = 0
5+
i = j = 0
6+
max = min = nums[i]
7+
window = [nums[i]]
8+
9+
loop do
10+
while i < j && max - min > 2
11+
x = window.shift
12+
max = window.max if max == x
13+
min = window.min if min == x
14+
15+
i += 1
16+
end
17+
count += j-i+1
18+
19+
j += 1
20+
break if j >= nums.size
21+
22+
window.push(nums[j])
23+
max = nums[j] if max < nums[j]
24+
min = nums[j] if min > nums[j]
25+
end
26+
27+
count
28+
end

0 commit comments

Comments
 (0)