Skip to content

Commit 4aed476

Browse files
committed
Runtime: 824 ms (Top 50.97%) | Memory: 32.5 MB (Top 61.17%)
1 parent c5462e2 commit 4aed476

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1+
# Runtime: 824 ms (Top 50.97%) | Memory: 32.5 MB (Top 61.17%)
12
class Solution:
23
def minSubarray(self, nums: List[int], p: int) -> int:
34
r = sum(nums)%p
45
if r == 0: return 0
5-
6-
d = {0:-1}
6+
7+
d = {0:-1}
78
s = 0
89
ans = None
9-
10+
1011
for i, n in enumerate(nums):
1112
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
2021
if target in d:
2122
a = i - d[target]
2223
if ans is None: ans = a
2324
else: ans = min(ans, a)
2425
if ans == len(nums) or ans is None: return -1
2526
return ans
26-

0 commit comments

Comments
 (0)