Class Solution
-
- All Implemented Interfaces:
public final class Solution2025 - Maximum Number of Ways to Partition an Array\.
Hard
You are given a 0-indexed integer array
numsof lengthn. The number of ways to partitionnumsis the number ofpivotindices that satisfy both conditions:1 <= pivot < nnums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]
You are also given an integer
k. You can choose to change the value of one element ofnumstok, or to leave the array unchanged.Return the maximum possible number of ways to partition
numsto satisfy both conditions after changing at most one element.Example 1:
Input: nums = 2,-1,2, k = 3
Output: 1
Explanation: One optimal approach is to change nums0 to k. The array becomes **3** ,-1,2.
There is one way to partition the array:
For pivot = 2, we have the partition 3,-1 | 2: 3 + -1 == 2.
Example 2:
Input: nums = 0,0,0, k = 1
Output: 2
Explanation: The optimal approach is to leave the array unchanged.
There are two ways to partition the array:
For pivot = 1, we have the partition 0 | 0,0: 0 == 0 + 0.
For pivot = 2, we have the partition 0,0 | 0: 0 + 0 == 0.
Example 3:
Input: nums = 22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14, k = -33
Output: 4
Explanation: One optimal approach is to change nums2 to k. The array becomes 22,4, **\-33** ,-20,-15,15,-16,7,19,-10,0,-13,-14.
There are four ways to partition the array.
Constraints:
n == nums.length<code>2 <= n <= 10<sup>5</sup></code>
<code>-10<sup>5</sup><= k, numsi<= 10<sup>5</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegerwaysToPartition(IntArray nums, Integer k)-
-
Method Detail
-
waysToPartition
final Integer waysToPartition(IntArray nums, Integer k)
-
-
-
-