Split Array Largest Sum

You are given an array nums of non-negative integers and an integer k.

Split nums into exactly k non-empty, contiguous pieces (every element belongs to exactly one piece, and the pieces keep the array's order). The cost of a split is the largest sum among its pieces.

Return the smallest cost any split can achieve.

Constraints

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 10^6
  • 1 <= k <= min(50, nums.length)

Examples

Example 1

Input: nums = [6,3,8,2,5], k = 2
Output: 15

Cutting after the 3 gives [6, 3] and [8, 2, 5], with sums 9 and 15. The other cuts give a largest sum of 18, 17 or 19, so 15 is the best.

Example 2

Input: nums = [4,4,4,4], k = 2
Output: 8

Cut in the middle: [4, 4] and [4, 4], both summing to 8. Any other cut leaves one piece with at least 12.

Example 3

Input: nums = [2,9,3], k = 3
Output: 9

With k equal to the length, every element is its own piece, so the largest piece is the largest element.

JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
function splitArray(nums, k) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.