HardBinary SearchArrayGreedy

Divide Chocolate

LeetCode
1 approach, code in all languages

You have a chocolate bar described by an array where each value is the sweetness of one consecutive chunk. You want to share it with k friends, so you make k straight cuts between chunks, producing k + 1 contiguous pieces. Every piece keeps its chunks together, and the sweetness of a piece is the sum of the chunks inside it.

Being generous, you always take the piece with the least total sweetness. Choose the cuts so that this smallest piece is as sweet as possible, and return that maximum achievable minimum sweetness.

Example 1

Input: sweetness = [1,2,3,4,5,6,7,8,9], k = 5

Output: 6

Cutting into pieces [1,2,3], [4,5], [6], [7], [8], [9] gives sweetness 6, 9, 6, 7, 8, 9. The weakest piece is 6, and no set of six pieces can raise that floor higher.

Example 2

Input: sweetness = [5,6,7,8,9,1,2,3,4], k = 8

Output: 1

With 8 cuts every chunk becomes its own piece, so the smallest piece is simply the smallest chunk, which is 1.

Constraints

  • 0 <= k < sweetness.length <= 10^4
  • 1 <= sweetness[i] <= 10^5
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