This is one of those problems where the clean idea matters more than the amount of code. For 1518. Water Bottles, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numWaterBottles.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(logN), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 33.56% of C++ online submissions for Water Bottles.
02//Memory Usage: 5.9 MB, less than 100.00% of C++ online submissions for Water Bottles.
03class Solution {
04public:
05 int numWaterBottles(int numBottles, int numExchange) {
06 int ans = 0;
07 int numEmptyBottles = 0;
08
09 while(numBottles){
10 ans += numBottles;
11 // cout << numBottles << ", " << numEmptyBottles << endl;
12 int total = numBottles+numEmptyBottles;
13 numBottles = total/numExchange;
14 numEmptyBottles = total%numExchange;
15 }
16
17 return ans;
18 }
19};
20
21//O(logN)
22//https://leetcode.com/problems/water-bottles/discuss/743148/JavaPython-3-O(logN)-and-O(1)-codes-w-brief-analysis.
23//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Water Bottles.
24//Memory Usage: 6 MB, less than 100.00% of C++ online submissions for Water Bottles.
25//time: O(logN), space: O(1)
26class Solution {
27public:
28 int numWaterBottles(int numBottles, int numExchange) {
29 //drink
30 int ans = numBottles;
31
32 //while we can exchange for a full bottle
33 while(numBottles >= numExchange){
34 //exchange and drink
35 ans += numBottles/numExchange;
36 numBottles = numBottles/numExchange + numBottles%numExchange;
37 }
38 return ans;
39 }
40};
41
42//O(1)
43//not understand
44//https://leetcode.com/problems/water-bottles/discuss/744623/Python-One-liner-explanation
45//Runtime: 4 ms, faster than 33.56% of C++ online submissions for Water Bottles.
46//Memory Usage: 6.1 MB, less than 100.00% of C++ online submissions for Water Bottles.
47class Solution {
48public:
49 int numWaterBottles(int numBottles, int numExchange) {
50 return numBottles + (numBottles-1)/(numExchange-1);
51 }
52};
Cost