Created
October 2, 2025 19:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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