Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Binary search

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Binary search

Split Array Largest Sum

advancedlayer B6 · Binary search5 tests

Given an array of non-negative integers nums and an integer k, cut the array into exactly k non-empty contiguous pieces. Each piece has a sum; the cost of a split is the largest of those sums. Return the smallest cost achievable.

  • This is binary search on the answer: search over candidate cost values, not over the array or over the possible cut positions
  • Candidate costs run from max(nums) to sum(nums), and 'can I split within this cost?' is monotone in the cost
  • Given a cost limit, greedily extend each piece until adding the next value would exceed it, and count how many pieces that takes
  • k is between 1 and nums.length

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen