There was an error while loading. Please reload this page.
2 parents 82baf8a + 73b3bf4 commit b262b08Copy full SHA for b262b08
2701-2800/2762_continuous_subarrays.rb
@@ -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
26
27
+ count
28
+end
0 commit comments