Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created October 2, 2025 19:44
Show Gist options
  • Select an option

  • Save tatsuyax25/e2c56d63866f92dfe6e75b0fafd0d919 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/e2c56d63866f92dfe6e75b0fafd0d919 to your computer and use it in GitHub Desktop.
You are given two integers numBottles and numExchange. numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations: Drink any number of full water bottles turning
/**
* @param {number} numBottles
* @param {number} numExchange
* @return {number}
*/
var maxBottlesDrunk = function(numBottles, numExchange) {
// Track the total number of bottles drunk
let totalDrunk = 0;
// Track how many empty bottles we have after drinking
let emptyBottles = 0;
// Continue as long as we have full bottles to drink
while (numBottles > 0) {
// Drink all current full bottles
totalDrunk += numBottles;
// They become empty bottles
emptyBottles += numBottles;
// We've drunk them all so reset full bottles
numBottles = 0;
// Check if we have enough empty bottles to exchange
if (emptyBottles >= numExchange) {
// Exchange once: spend numExchange empty bottles to get 1 full bottle
emptyBottles -= numExchange;
numBottles += 1;
// Increase the cost of exchange for next time
numExchange += 1;
} else {
// Not enough empty bottles to exchange, so we're done
break;
}
}
// Return the total number of bottles we've drunk
return totalDrunk;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment