Minimize the feasible answer
Updated
Minimum feasible speed is the first speed with hours(speed) <= H. Bound-search the speed range: if hours(mid) > H then L = mid+1 else R = mid. Loop while L < R. Return L.
What to focus on
For a = [4, 1, 9] and H = 5 the answer is 4. Do not return a pile index.
Worked examples
H=5 → speed 4
a = [4, 1, 9], H = 5- L=1 R=9 mid=5 hours=4 <=5 so R=5
- L=1 R=5 mid=3 hours=6 >5 so L=4
- L=4 R=5 mid=4 hours=5 <=5 so R=4
- L=4 R=4
Result: min speed 4
H=8 → speed 2
a = [4, 1, 9], H = 8- L=1 R=9 mid=5 hours=4 <=8 so R=5
- L=1 R=5 mid=3 hours=6 <=8 so R=3
- L=1 R=3 mid=2 hours=8 <=8 so R=2
- L=1 R=2 mid=1 hours=14 >8 so L=2
- L=2 R=2
Result: min speed 2
H=14 → speed 1
a = [4, 1, 9], H = 14- L=1 R=9 mid=5 hours=4 <=14 so R=5
- keep shrinking the feasible side until L=1
- hours(1)=14 <=14
Result: min speed 1
Try it
Write minSpeed(a, H). a is the pile array. H is the hour limit. L, mid, and R are integer speeds, not pile indices. If hours(mid) > H then L = mid+1, else R = mid. Return the minimum feasible speed. [4, 1, 9] H=5 → 4 H=8 → 2 H=14 → 1
Piles a are positive integers, non-empty. H is large enough that some speed in 1..max(a) is feasible. L, mid, and R are speeds.