Most crates loaded: hints, no solution
Easy · Greedy
A food truck has a weight limit for its cargo bay. You are given the weights of the crates waiting on the dock and the limit, both in kilograms. Each crate can be loaded at most once, and the total weight of the loaded crates must not exceed the limit. Return the greatest count of crates that can be loaded. If not even the lightest crate fits, return 0. Crates differ only in weight, and the order of loading does not matter.
Examples
- Input
- weights = [5, 2, 9, 3, 4], limit = 10
- Output
- 3
- Note
- The 2, 3 and 4 kg crates weigh 9 kg together. Adding any other crate goes over 10.
- Input
- weights = [7, 8], limit = 6
- Output
- 0
- Note
- Every crate is heavier than the limit.
- Input
- weights = [4, 4, 4, 4], limit = 8
- Output
- 2
Constraints
- 0 <= weights.length <= 180000
- 1 <= weights[i] <= 10000
- 0 <= limit <= 100000000
Hint 1: a nudge
If you could only load one crate, which one would you pick? Does the answer change once you want to load two or three?
Unlock the full hints and timed practice with a pass
The second and third hints and the timed, runner-checked practice are part of a pass.