Skip to content
LC-3381 Medium LeetCode

3381. Maximum Subarray Sum With Length Divisible by K

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 27% Topics: Array, Hash Table, Prefix Sum
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(k)

# prefix sum, dp
class Solution(object):
    def maxSubarraySum(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        dp = [float("inf")]*k
        dp[-1] = 0
        curr = 0
        result = float("-inf")
        for i, x in enumerate(nums):
            curr += x
            result = max(result, curr-dp[i%k])
            dp[i%k] = min(dp[i%k], curr)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT