2025. Maximum Number of Ways to Partition an Array

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2025

class Solution:
    def waysToPartition(self, nums: List[int], k: int) -> int:
        n = len(nums)
        s = [nums[0]] * n
        right = defaultdict(int)
        for i in range(1, n):
            s[i] = s[i - 1] + nums[i]
            right[s[i - 1]] += 1

        res = 0
        if s[-1] % 2 == 0:
            res = right[s[-1] // 2]

        left = defaultdict(int)
        for v, x in zip(s, nums):
            d = k - x
            if (s[-1] + d) % 2 == 0:
                t = left[(s[-1] + d) // 2] + right[(s[-1] - d) // 2]
                if res < t:
                    res = t
            left[v] += 1
            right[v] -= 1
        return res