File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def countGood (self , nums , k ):
3
+ """
4
+ :type nums: List[int]
5
+ :type k: int
6
+ :rtype: int
7
+ """
8
+ elements_in_current_window = dict ()
9
+ pair_count = 0
10
+ start = 0
11
+ end = 0
12
+ ans = 0
13
+ while (end < len (nums )):
14
+ if (elements_in_current_window .has_key (nums [end ])):
15
+ elements_in_current_window [nums [end ]]+= 1 #incrementing
16
+ else :
17
+ elements_in_current_window [nums [end ]]= 1 #initializing entry in the dictionary
18
+ pair_count += elements_in_current_window [nums [end ]]- 1 #counting number of pairs
19
+ while pair_count >= k : #The shrinking phase
20
+ ans += 1 + len (nums )- end - 1 #the number of new sequences
21
+ elements_in_current_window [nums [start ]]-= 1 #remove the element at the starting of the window
22
+ pair_count -= elements_in_current_window [nums [start ]]
23
+ start += 1
24
+ end += 1
25
+ return ans
26
+
27
+
You can’t perform that action at this time.
0 commit comments