Bookshelf
InterviewTime limit1sMemory limit128 MB
Given cow heights and a shelf height B, find the smallest number of cows whose heights sum to at least B.
- Level
Easy3 of 10
- Topics
- Greedy, Sorting, Array, Implementation
- Solved
- No attempts yet
Problem
Farmer John recently bought a bookshelf for the cow library, but the shelf fills up quickly, and now the only free space left is at the very top.
There are cows (), and each cow has a height (). Let be the sum of all cow heights. The bookshelf has a height of ().
To reach the top of the bookshelf, which is taller than the tallest cow, one or more cows can stand on top of each other in a stack. The total height of a stack equals the sum of the individual heights of its cows, and this total must be at least the bookshelf height . Because stacking more cows than necessary is dangerous, find the smallest number of cows in a stack that still reaches the bookshelf.
Input
- Line 1: Two space-separated integers, and
- Lines 2 to : Line contains a single integer .
Output
- Line 1: A single integer, the size of the smallest set of cows that can reach the bookshelf.
Hint
For example, when the bookshelf height is , one way to reach it with cows is ; many other ways exist.