/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2025年11月27日 星期四

Py3 C C++ prefix sum解Leetcode 3381 Maximum Subarray Sum With Length Div...


Py3 C C++ prefix sum解Leetcode 3381  Maximum Subarray Sum With Length Divisible by K
Very similar to Kadane's algorithm
[Py3 code請進]




class Solution:
    def maxSubarraySum(self, nums: List[int], k: int) -> int:
        n=len(nums)
        minS=[inf]*(k-1)+[0]
        prefix, ans=0, -inf
        for i, x in enumerate(nums):
            prefix+=x
            ik=i%k
            ans=max(ans, prefix-minS[ik])
            minS[ik]=min(prefix, minS[ik])
        return ans

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章