Water bottle Problem (Leetcode POTD)
Problem Link
Approach
We need to calculate the maximum number of full water bottles we can consume right so let's consider one test case
Total filled bottled = 15, Exchange Rate = 4
So initially we will consume 15 bottles now these bottles are empty, so keeping in mind the exchange rate of 4, we can obtain 15/3 = 3 bottles and we are left with 3 more empty bottle, now after consuming all we are left with 6 empty bottles, so how many new bottles we can get out of this?
The answer is 6/4 = 1 and we are left with 2 empty bottles and even after consuming the filled bottles we now have 3 empty bottles and since the exchange rate is 4 we can't proceed further
Hence 15+3+1 = 19 is the ans.
Code
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
int ans = 0;
while(numBottles!=1){
ans = ans+numBottles;
numBottles = numBottles/numExchange+numExchange%numBottles;
}
return numBottles;
}
};
Stay hydrated 🤣🤣
Ujjwal sharma
Subscribe to my newsletter
Read articles from Ujjwal Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by