HardArrayBinary SearchGreedy

Split Array Largest Sum

LeetCode
1 approach, code in all languages

You are given an array of non-negative integers and a positive integer k. Cut the array into exactly k contiguous, non-empty pieces without reordering any elements. Every element must belong to exactly one piece.

Among all the ways to make these cuts, look at the piece whose element sum is the largest. Your goal is to arrange the cuts so that this largest piece sum is as small as possible, and return that minimized value.

Example 1

Input: nums = [7, 2, 5, 10, 8], k = 2

Output: 18

The best split is [7, 2, 5] and [10, 8], giving piece sums of 14 and 18. The largest is 18, and no other 2-way split lowers that maximum.

Example 2

Input: nums = [1, 4, 4], k = 3

Output: 4

Each element becomes its own piece: [1], [4], [4]. The largest piece sum is 4, which cannot be reduced with three pieces.

Constraints

  • 1 <= nums.length <= 10^3
  • 0 <= nums[i] <= 10^6
  • 1 <= k <= nums.length
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now