Skip to content

← leetcode

Water Bottles II

· Medium

LeetCode problem 3100

It is just a simulation question; just follow the steps provided in the question directly and you will reach the answer! (the constraints provided are tiny)

code

class Solution {
public:
    int maxBottlesDrunk(int numBottles, int numExchange) {
        int ans = numBottles;
        while (++numBottles > numExchange) {
            ++ans;
            numBottles -= numExchange++;
        }
        return ans;
    }
};

(could technically solve this using math too but like this is waaaaay easier)

complexity

  • Time: O(sqrt(n))
  • Space: O(1)

time taken

  • 3 minutes and 23 seconds