410. Split Array Largest Sum LeetCode Solution
In this guide, you will get 410. Split Array Largest Sum LeetCode Solution with the best time and space complexity. The solution to Split Array Largest Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Split Array Largest Sum solution in C++
- Split Array Largest Sum solution in Java
- Split Array Largest Sum solution in Python
- Additional Resources
Problem Statement of Split Array Largest Sum
Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized.
Return the minimized largest sum of the split.
A subarray is a contiguous part of the array.
Example 1:
Input: nums = [7,2,5,10,8], k = 2
Output: 18
Explanation: There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
Example 2:
Input: nums = [1,2,3,4,5], k = 2
Output: 9
Explanation: There are four ways to split nums into two subarrays.
The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
Constraints:
1 <= nums.length <= 1000
0 <= nums[i] <= 106
1 <= k <= min(50, nums.length)
Complexity Analysis
- Time Complexity: O(kn^2)
- Space Complexity: O(nk)
410. Split Array Largest Sum LeetCode Solution in C++
class Solution {
public:
int splitArray(vector<int>& nums, int k) {
const int n = nums.size();
vector<vector<int>> mem(n + 1, vector<int>(k + 1, INT_MAX));
vector<int> prefix(n + 1);
partial_sum(nums.begin(), nums.end(), prefix.begin() + 1);
return splitArray(nums, n, k, prefix, mem);
}
private:
// Returns the minimum of the maximum sum to split the first i numbers into k
// groups.
int splitArray(const vector<int>& nums, int i, int k,
const vector<int>& prefix, vector<vector<int>>& mem) {
if (k == 1)
return prefix[i];
if (mem[i][k] < INT_MAX)
return mem[i][k];
// Try all the possible partitions.
for (int j = k - 1; j < i; ++j)
mem[i][k] = min(mem[i][k], max(splitArray(nums, j, k - 1, prefix, mem),
prefix[i] - prefix[j]));
return mem[i][k];
}
};
/* code provided by PROGIEZ */
410. Split Array Largest Sum LeetCode Solution in Java
class Solution {
public int splitArray(int[] nums, int k) {
final int n = nums.length;
int[][] mem = new int[n + 1][k + 1];
int[] prefix = new int[n + 1];
Arrays.stream(mem).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));
for (int i = 0; i < n; ++i)
prefix[i + 1] = nums[i] + prefix[i];
return splitArray(nums, n, k, prefix, mem);
}
// Returns the minimum of the maximum sum to split the first i numbers into k
// groups.
private int splitArray(int[] nums, int i, int k, int[] prefix, int[][] mem) {
if (k == 1)
return prefix[i];
if (mem[i][k] < Integer.MAX_VALUE)
return mem[i][k];
// Try all the possible partitions.
for (int j = k - 1; j < i; ++j)
mem[i][k] = Math.min(mem[i][k], //
Math.max(splitArray(nums, j, k - 1, prefix, mem), //
prefix[i] - prefix[j]));
return mem[i][k];
}
}
// code provided by PROGIEZ
410. Split Array Largest Sum LeetCode Solution in Python
class Solution:
def splitArray(self, nums: list[int], k: int) -> int:
prefix = list(itertools.accumulate(nums, initial=0))
@functools.lru_cache(None)
def dp(i: int, k: int) -> int:
"""
Returns the minimum of the maximum sum to split the first i numbers into
k groups.
"""
if k == 1:
return prefix[i]
return min(max(dp(j, k - 1), prefix[i] - prefix[j])
for j in range(k - 1, i))
return dp(len(nums), k)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.