Solved LeetCode program · 775 of 1,000
#1011 · Capacity To Ship Packages Within D Days
Original English source
Keep a shared weights holding 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Keep a shared days of 0. Keep a shared fits of 0.
To test with capacity (a whole number). Set fits to 1. The used is 0. The needed is 1.
For every spot from 0 to the last position of weights.
The parcel is item spot of weights. If used plus parcel is more than capacity, add 1 to needed, set used to 0. Add parcel to used. If needed is more than days, set fits to 0, stop.
Set days to 5. The low is 1.
For each parcel in weights.
If parcel is more than low, set low to parcel.
The high is 0.
For each parcel in weights.
Add parcel to high.
Repeat while low is less than high.
The middle is the midpoint of low and high. Test with middle. If fits is 1, set high to middle, otherwise set low to middle plus 1.
Show low.
Expected stdout
15