其他分享
首页 > 其他分享> > leetcode-1590-使数组和能被P整除

leetcode-1590-使数组和能被P整除

作者:互联网

 

 

 

 

 

 方法一:

INF = 1 << 60
class Solution:
    def minSubarray(self, A: List[int], p: int) -> int:
        idx = {}
        n = len(A)
        S = [0] * (n + 1)
        for i in range(n):
            S[i + 1] = S[i] + A[i]
        s = S[-1] % p
        if s == 0:
            return 0
        
        ans = INF
        for i, v in enumerate(S):
            l = (v - s) % p
            if l in idx:
                ans = min(ans, i - idx[l])
            idx[v % p] = i
        
        if ans == INF or ans == n:
            ans = -1
        return ans

 

标签:return,idx,min,int,leetcode,1590,ans,INF,整除
来源: https://www.cnblogs.com/oldby/p/13711348.html