1590. Make Sum Divisible by P

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1590

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