[코테 풀이] Water Bottles

시내·2024년 7월 29일
0

Q_1518) Water Bottles

출처 : https://leetcode.com/problems/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.

class Solution {
    public int numWaterBottles(int numBottles, int numExchange){
        int total = numBottles;
        int newlyFilled = 0, oldEmpty = 0, updated = numBottles;
        boolean condition = true;
        while (condition) {
            if (updated < numExchange) condition = false;
            else {
                newlyFilled = updated / numExchange;
                oldEmpty = updated - numExchange * newlyFilled;
                total += newlyFilled;
                updated = newlyFilled + oldEmpty;
            }
        }
        return total;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글