You are given an initial number of bottles and an initial amount of money. You may either recycle bottles to earn money or spend bottles and money to buy perks.
Recycling one bottle gives recycleVal dollars. Buying one perk costs exactly 1 bottle and perkVal dollars.
Each bottle can be used at most once. A bottle that is recycled cannot be used to buy a perk, and a bottle used to buy a perk cannot be recycled. Return the maximum number of perks that can be obtained.
Method Signature
int maximumPerks(int numBottles, int dollars, int recycleVal, int perkVal)
numBottles is the initial number of bottles.
dollars is the initial amount of money available.
recycleVal is the number of dollars earned by recycling one bottle.
perkVal is the number of dollars needed along with one bottle to buy one perk.
Return Value
Return an integer representing the maximum number of perks that can be bought.
Constraints
0 ≤ numBottles ≤ 1,000,000,000
0 ≤ dollars ≤ 1,000,000,000
0 ≤ recycleVal ≤ 1,000,000,000
0 ≤ perkVal ≤ 1,000,000,000
- All input values are valid integers.
- The output must be deterministic and must always be the maximum possible number of perks.
Examples
Example 1
maximumPerks(numBottles = 5, dollars = 4, recycleVal = 2, perkVal = 3)
Output: 2
Explanation: Recycle 3 bottles to earn 6 dollars. Now there are 2 bottles and 10 dollars, so 2 perks can be bought. Buying 3 perks is impossible because only 2 bottles could be recycled, giving 8 dollars total, but 3 perks need 9 dollars.
Example 2
maximumPerks(numBottles = 4, dollars = 1, recycleVal = 5, perkVal = 6)
Output: 1
Explanation: Recycle 3 bottles to earn 15 dollars. Now there is 1 bottle and 16 dollars, so 1 perk can be bought. Buying 2 perks is impossible because only 2 bottles could be recycled, giving 11 dollars total, but 2 perks need 12 dollars.
Example 3
maximumPerks(numBottles = 6, dollars = 0, recycleVal = 1, perkVal = 0)
Output: 6
Explanation: Since each perk needs 0 dollars, every bottle can be used to buy one perk.
Example 4
maximumPerks(numBottles = 4, dollars = 10, recycleVal = 0, perkVal = 3)
Output: 3
Explanation: Recycling gives no money because recycleVal is 0. With 10 dollars and 4 bottles, at most 3 perks can be bought.