1518. Water Bottles
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : July 07, 2024
Last updated : July 07, 2024
Related Topics : Math, Simulation
Acceptance Rate : 70.99 %
n=9, k=3 13 = 9 + 3 + 1 n=15, k=4 ans = 15 + (15//4 + 15 % 4) + ... = 15 + ... = 19
(n bottles) + (n // k new bottles from trade) + ((n // k + n % k) // k new bottles from 2nd round of trading) + ((n // k + n % k) // k + (n // k + n % k) % k) // k + ... n = k * m + r1 m = k * n + r2 n = k * o + r3 ... (k * m) + ...
- e1518 v1 O(n) Daily.py
- e1518 v2 O(1).py
- e1518 v2.c
- e1518 v2.cpp
- e1518 v2.cs
- e1518 v2.java
- e1518 v2.js
- e1518 v2.ts
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
output = 0
while numBottles >= numExchange :
output += numBottles - numBottles % numExchange
numBottles = numBottles % numExchange + numBottles // numExchange
output += numBottles
return output
class Solution:
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
return numBottles + (numBottles - 1) // (numExchange - 1)
int numWaterBottles(int numBottles, int numExchange) {
return numBottles + (numBottles - 1) / (numExchange - 1);
}
class Solution {
public:
int numWaterBottles(int numBottles, int numExchange) {
return numBottles + (numBottles - 1) / (numExchange - 1);
}
};
public class Solution {
public int NumWaterBottles(int numBottles, int numExchange) {
return numBottles + (numBottles - 1) / (numExchange - 1);
}
}
class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
return numBottles + (numBottles - 1) / (numExchange - 1);
}
}
/**
* @param {number} numBottles
* @param {number} numExchange
* @return {number}
*/
var numWaterBottles = function(numBottles, numExchange) {
return numBottles + Math.floor((numBottles - 1) / (numExchange - 1));
};
function numWaterBottles(numBottles: number, numExchange: number): number {
return numBottles + Math.floor((numBottles - 1) / (numExchange - 1));
};