Re: [閒聊] 每日leetcode

作者: smart0eddie (smart0eddie)   2024-07-07 11:12:21
2024-07-07
1518. Water Bottles
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.
暴力解
每次多喝 B 瓶
每 E 個空瓶可以去換一瓶滿的
會剩下 R 個空瓶
int numWaterBottles(int numBottles, int numExchange) {
int count = numBottles;
int rest = 0;
int tmp_numBottles = 0;
while (numBottles + rest >= numExchange) {
tmp_numBottles = (numBottles + rest) / numExchange;
rest = (numBottles + rest) % numExchange;
count += tmp_numBottles;
numBottles = tmp_numBottles;
}
return count;
}
一行的數學姐看不懂
有大師能用姆咪也看得懂的方式解釋一下嗎
[Python] One line Math Solution O(1) beating 100%

Links booklink

Contact Us: admin [ a t ] ucptt.com