- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2444-count-subarrays-with-fixed-bounds.rb
47 lines (43 loc) · 1.04 KB
/
2444-count-subarrays-with-fixed-bounds.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
# 2444. Count Subarrays With Fixed Bounds
# https://leetcode.com/problems/count-subarrays-with-fixed-bounds
# Hard
# @param {Integer[]} nums
# @param {Integer} min_k
# @param {Integer} max_k
# @return {Integer}
defcount_subarrays(nums,min_k,max_k)
max_found=min_found=false
start=0
res=0
min_index=max_index=0
nums.each_with_indexdo |num,i|
unlessnum.between?(min_k,max_k)
start=i + 1
min_found=max_found=false
else
ifnum == min_k
min_found=true
min_index=i
end
ifnum == max_k
max_found=true
max_index=i
end
ifmin_found && max_found
res += ([min_index,max_index].min - start + 1)
end
end
end
res
end
# ********************#
# TEST #
# ********************#
require"test/unit"
classTest_count_subarrays < Test::Unit::TestCase
deftest_
assert_equal2,count_subarrays([1,3,5,2,7,5],1,5)
assert_equal10,count_subarrays([1,1,1,1],1,1)
end
end