← 返回 amazon 的题目列表Max Number of Products Taken
类型:online_judge
Given an array representing the stock of each product, where each element is the available stock. For example, a stock of [1, 2, 3, 5]. Each time you can choose k different products, starting from k = 1, then k = 2, and so on. After each selection, the stock of the selected products decreases accordingly. How many times can you perform the selection? Design and implement an algorithm to find this count.
Input
An integer array products representing the stock of each product.
Output
An integer representing the maximum number of selections that can be performed.
Example
Assume the products array is [1, 2, 3, 5].
The first selection is 1 product, you can choose any product, the stock becomes [0, 2, 3, 5].
The second selection is 2 products, you can choose the stocks [2] and [3], the stock becomes [0, 1, 2, 5].
The third selection is 3 products, you can choose the stocks [1], [2], and [5], the stock becomes [0, 0, 1, 4].
The fourth selection is 4 products, and so forth.
Until you cannot select enough products, the total number of selections performed is the result.
Example
Input
1 2 3 5