Problem · Array

Split Array Largest Sum

MediumSalesforceFULLTIMEOA
See Salesforce hiring insights

Given an integer array nums of size n and an integer k, split nums into k non-empty contiguous subarrays such that the sum of maximum values of each subarray is minimized.

Function Description

Complete the function splitArray in the editor.

splitArray has the following parameters:

  1. 1. int[] nums: an integer array
  2. 2. int k: the number of subarrays to split into

Returns

int: the minimum sum of the maximum values of the k subarrays

Examples
01 · Example 1
nums = [1, 2, 3, 4, 5]
k = 2
return = 6

The optimal split is [1], [2, 3, 4, 5]. The sum of the maximum values of each subarray is max([1]) + max([2, 3, 4, 5]) = 1 + 5 = 6.

Constraints
  • 0 <= k <= 300
  • 0 <= n <= 300
  • 0 <= nums[i] <= 10^5
More Salesforce problems
drafts saved locally
public int splitArray(int[] nums, int k) {
  // write your code here
}
nums[1, 2, 3, 4, 5]
k2
expected6
sign in to submit