1590. Make Sum Divisible by P
On This Page
class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
k = sum(nums) % p
if k == 0:
return 0
last = {0: -1}
cur = 0
res = len(nums)
for i, x in enumerate(nums):
cur = (cur + x) % p
target = (cur - k + p) % p
if target in last:
res = min(res, i - last[target])
last[cur] = i
return -1 if res == len(nums) else res