Leetcode 1694 Solution
This article provides solution to leetcode question 1694 (make-sum-divisible-by-p)
Access this page by simply typing in "lcs 1694" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/make-sum-divisible-by-p
Solution
class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
target = sum(nums) % p
if target == 0:
return 0
m = {0: -1}
s = 0
ans = len(nums)
for i, num in enumerate(nums):
s += num
if (s - target + p) % p in m:
ans = min(ans, i - m[(s - target + p) % p])
m[s % p] = i
return ans if ans != len(nums) else -1