-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_subarrays_with_fixed_bounds.py
54 lines (46 loc) · 1.76 KB
/
count_subarrays_with_fixed_bounds.py
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
48
49
50
51
52
53
54
'''
You are given an integer array nums and two integers minK and maxK.
A fixed-bound subarray of nums is a subarray that satisfies the following conditions:
The minimum value in the subarray is equal to minK.
The maximum value in the subarray is equal to maxK.
Return the number of fixed-bound subarrays.
A subarray is a contiguous part of an array.
'''
def countSubarrays(self, A: List[int], minK: int, maxK: int) -> int:
res = 0
jmin = jmax = jbad = -1
for i,a in enumerate(A):
if not minK <= a <= maxK: jbad = i
if a == minK: jmin = i
if a == maxK: jmax = i
res += max(0, min(jmin, jmax) - jbad)
return res
---------------------------------------------------------------------------------------
class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
def split(seq):
start = 0
for idx, num in enumerate(seq):
if num < minK or num > maxK:
if idx != start:
yield seq[start:idx]
start = idx + 1
idx += 1
if idx != start:
yield seq[start:idx]
def count(seq):
nmi = -1 # index of nearest/rightest minK so far
nma = -1 # index of nearest/rightest maxK so far
ans = 0
for idx, num in enumerate(seq):
if num == minK:
nmi = idx
if num == maxK:
nma = idx
if nmi != -1 and nma != -1:
ans += min(nmi, nma) + 1
return ans
ans = 0
for seq in split(nums):
ans += count(seq)
return ans