diff options
-rw-r--r-- | water_bottles.c | 26 | ||||
-rw-r--r-- | water_bottles.py | 25 |
2 files changed, 51 insertions, 0 deletions
diff --git a/water_bottles.c b/water_bottles.c new file mode 100644 index 0000000..20a0a2d --- /dev/null +++ b/water_bottles.c @@ -0,0 +1,26 @@ +// 1518. Water Bottles +#include "leetcode.h" + +/* + * there are 'numBottles' water bottles that are initially full of water. you + * can exchange 'numExchange' empty water botls from the market with one full + * water bottle. the operation of drinking a full water bottle turns into an + * empty bottle. given the two integers 'numBottles' and 'numExchange', return + * the maximum number of water bottles you can drink + */ + +int numWaterBottles(int numBottles, int numExchange) { + int ans = numBottles; + while (numBottles >= numExchange) { + int rem = numBottles % numExchange; + numBottles /= numExchange; + ans += numBottles; + numBottles += rem; + } + return ans; +} + +int main() { + printf("%d\n", numWaterBottles(9, 3)); // expect: 13 + printf("%d\n", numWaterBottles(15, 4)); // expect: 19 +} diff --git a/water_bottles.py b/water_bottles.py new file mode 100644 index 0000000..0ada9f7 --- /dev/null +++ b/water_bottles.py @@ -0,0 +1,25 @@ +# 1518. Water Bottles + +""" +there are 'numBottles' water bottles that are initially full of water. you +can exchange 'numExchange' empty water botls from the market with one full +water bottle. the operation of drinking a full water bottle turns into an +empty bottle. given the two integers 'numBottles' and 'numExchange', return +the maximum number of water bottles you can drink +""" + + +class Solution(object): + def numWaterBottles(self, numBottles, numExchange): + """ + :type numBottles: int + :type numExchange: int + :rtype: int + """ + return (numBottles * numExchange - 1) // (numExchange - 1) + + +if __name__ == "__main__": + obj = Solution() + print(obj.numWaterBottles(9, 3)) + print(obj.numWaterBottles(15, 4)) |