File tree 1 file changed +12
-12
lines changed
scripts/algorithms/M/Make Sum Divisible by P
1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 824 ms (Top 50.97%) | Memory: 32.5 MB (Top 61.17%)
1
2
class Solution :
2
3
def minSubarray (self , nums : List [int ], p : int ) -> int :
3
4
r = sum (nums )% p
4
5
if r == 0 : return 0
5
-
6
- d = {0 :- 1 }
6
+
7
+ d = {0 :- 1 }
7
8
s = 0
8
9
ans = None
9
-
10
+
10
11
for i , n in enumerate (nums ):
11
12
s = (s + n )% p
12
-
13
- # save all possible remainder with latest index only
14
- d [s ] = i
15
-
16
- # search the target remainder with index closest to current i
17
- # s is the current remainder so we can derive such relation:
18
- # s - target = r (mod p) => target = s-r (mod p)
19
- target = (s - r )% p
13
+
14
+ # save all possible remainder with latest index only
15
+ d [s ] = i
16
+
17
+ # search the target remainder with index closest to current i
18
+ # s is the current remainder so we can derive such relation:
19
+ # s - target = r (mod p) => target = s-r (mod p)
20
+ target = (s - r )% p
20
21
if target in d :
21
22
a = i - d [target ]
22
23
if ans is None : ans = a
23
24
else : ans = min (ans , a )
24
25
if ans == len (nums ) or ans is None : return - 1
25
26
return ans
26
-
You can’t perform that action at this time.
0 commit comments