Re: [闲聊] 每日leetcode

楼主: DJYOSHITAKA (Evans)   2024-10-03 22:21:34
写的乱乱的
就一些prefix sum搞来搞去
明天乖乖上班
哀哀哀
def minSubarray(self, nums: List[int], p: int) -> int:
res = 0
for num in nums:
res += num
res = res%p
cur_sum = 0
mp = {}
mp[0] = -1
ans = 10**6
for i, num in enumerate(nums):
cur_sum += num
cur_res = cur_sum%p
mp[cur_res] = i
target_res = (cur_res-res+p)%p
if target_res in mp:
ans = min(ans, i-mp[target_res])
if ans==10**6 or ans==len(nums):
return -1
else:
return ans
作者: nozomizo (希霙)   2024-10-03 22:25:00
别卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com