Water Bottles
Problem
There are numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.
The operation of drinking a full water bottle turns it into an empty bottle.
Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.
Examples
Example 1:
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.
Lets assume ● is filled bottle, and ○ is empty bottle. Lets denote D for drink and X for exchange, then this is how it will work.
Initial
● ● ● ○ ○ ○ ● ○
● ● ● =D=> ○ ○ ○ =X=> ● =D=> ○ =X=> ● =D=> ○
● ● ● ○ ○ ○ ● ○
Example 2:
Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 15 + 3 + 1 = 19.
Solution
Method 1 - Iterative
As long as there are at least numExchange bottles remaining, we can drink them and exchange the empty bottles for full ones.
Code
C++
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
int ans = numBottles;
while (numBottles >= numExchange) {
int exchange = numBottles / numExchange;
ans += exchange;
numBottles = exchange + (numBottles % numExchange);
}
return ans;
}
};
Go
package main
func numWaterBottles(numBottles int, numExchange int) int {
ans := numBottles
for numBottles >= numExchange {
exchange := numBottles / numExchange
ans += exchange
numBottles = exchange + (numBottles % numExchange)
}
return ans
}
Java
class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
int ans = numBottles;
while (numBottles >= numExchange) {
int remainder = numBottles % numExchange;
numBottles /= numExchange;
ans += numBottles;
numBottles += remainder;
}
return ans;
}
}
Python
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
ans = numBottles
while numBottles >= numExchange:
exchange = numBottles // numExchange
ans += exchange
numBottles = exchange + (numBottles % numExchange)
return ans
Kotlin
class Solution {
fun numWaterBottles(numBottles: Int, numExchange: Int): Int {
var bottles = numBottles
var ans = bottles
while (bottles >= numExchange) {
val exchange = bottles / numExchange
ans += exchange
bottles = exchange + (bottles % numExchange)
}
return ans
}
}
Rust
impl Solution {
pub fn num_water_bottles(mut num_bottles: i32, num_exchange: i32) -> i32 {
let mut ans = num_bottles;
while num_bottles >= num_exchange {
let exchange = num_bottles / num_exchange;
ans += exchange;
num_bottles = exchange + (num_bottles % num_exchange);
}
ans
}
}
TypeScript
function numWaterBottles(numBottles: number, numExchange: number): number {
let ans = numBottles;
while (numBottles >= numExchange) {
const exchange = Math.floor(numBottles / numExchange);
ans += exchange;
numBottles = exchange + (numBottles % numExchange);
}
return ans;
}
Complexity
- ⏰ Time complexity:
O(log n), wherenis number of bottles - 🧺 Space complexity:
O(1)
Method 2 - Geometric Progression
We know that in Geometric Progression, when the progression is converging, then the formula is:
For eg. when r = 0.5 = 1/2, this is GP is converging.
These are the terms of GP for eg. 1:
t0 = 9
t1 = 3
t2 = 1
So, we can see that a = 9, r = 1/3,
S = a / (1 - r) = 9 / (1 - 1/3) = 9 / 2/3 = 9 * 3 / 2 = 27 / 2 = 13
Note, answer is not 13.5, but 13, as we want 1 complete bottle, not half empty bottles.
So, this example worked, but we need to make sure we do integer division. So, lets derive the formula. Let n be initial bottles given, and k be number of bottles we exchange to get 1 full bottle. First exchange:
Remaining Empty Bottles:
Number of Full Bottles After Each Exchange:
The total number of bottles you can drink is the sum of the initial bottles and the total number of full bottles obtained through exchanges. To simplify this calculation, we use the formula:
To derive the total exchanges, realize that every additional bottle obtained through exchanges adds:
Simplified Geometric Series - Combining all, the total number of bottles you can drink simplifies to:
Code
C++
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
if (numExchange <= 1) return numBottles;
return numBottles + (numBottles - 1) / (numExchange - 1);
}
};
Go
package main
func numWaterBottles(numBottles int, numExchange int) int {
if numExchange <= 1 {
return numBottles
}
return numBottles + (numBottles-1)/(numExchange-1)
}
Java
class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
return numBottles + (numBottles - 1) / (numExchange - 1);
}
}
Kotlin
class Solution {
fun numWaterBottles(numBottles: Int, numExchange: Int): Int {
if (numExchange <= 1) return numBottles
return numBottles + (numBottles - 1) / (numExchange - 1)
}
}
Python
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
if numExchange <= 1:
return numBottles
return numBottles + (numBottles - 1) // (numExchange - 1)
Rust
impl Solution {
pub fn num_water_bottles(num_bottles: i32, num_exchange: i32) -> i32 {
if num_exchange <= 1 { return num_bottles; }
num_bottles + (num_bottles - 1) / (num_exchange - 1)
}
}
TypeScript
function numWaterBottlesFormula(numBottles: number, numExchange: number): number {
if (numExchange <= 1) return numBottles;
return numBottles + Math.floor((numBottles - 1) / (numExchange - 1));
}
Complexity
- ⏰ Time complexity:
O(1) - 🧺 Space complexity:
O(1)