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

Capacity to Ship Packages Within D Days

advancedlayer B6 · Binary search5 tests

Packages with the given weights must be loaded onto a boat in the order listed. Each day the boat carries a prefix of what is left, never exceeding its weight capacity. Return the smallest capacity that gets every package shipped within days days.

  • This is binary search on the answer: the search space is the range of candidate capacities, not the input array
  • The low end is max(weights) — anything smaller can never carry that one package. The high end is sum(weights) — ship everything in one day
  • Feasibility is monotone: if a capacity works, every larger capacity works
  • Order is fixed; you may not reorder packages

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