Skip to content

Commit f576a27

Browse files
committed
Resolved 'Count Subarrays With Fixed Bounds' LeetCode problem with an optimized solution
1 parent 1513148 commit f576a27

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Count Subarrays With Fixed Bounds
2+
3+
class Solution(object):
4+
def countSubarrays(self, nums, minK, maxK):
5+
res = 0
6+
bad_idx = left_idx = right_idx = -1
7+
8+
for i, num in enumerate(nums) :
9+
if not mink <= num <= maxK:
10+
bad_idx = i
11+
12+
if num == mink:
13+
left_idx = i
14+
15+
if num == maxK:
16+
right_idx = i
17+
18+
res += max(0, min(left_idx, right_idx) - bad_idx)
19+
20+
return res
21+
22+
solution = Solution()
23+
print(solution.countSubarrays([1,3,5,2,7,5], 1, 5))

0 commit comments

Comments
 (0)