Skip to content

Commit 5a2f64c

Browse files
chore: add LeetCode daily solution
1 parent 7c6a459 commit 5a2f64c

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Water Bottles II (Medium)
2+
3+
**Problem ID:** 3100
4+
**Date:** 2025-10-02
5+
**Link:** https://leetcode.com/problems/water-bottles-ii/
6+
7+
## Approach
8+
9+
To solve the "Water Bottles II" problem, we can use a greedy approach to maximize the number of water bottles consumed through drinking and exchanging empty bottles for full ones.
10+
11+
### Main Idea:
12+
1. **Initial Setup**: Start with the given number of full bottles (`numBottles`) and initialize a counter for the total number of bottles drunk.
13+
2. **Drinking and Exchanging**: In each iteration:
14+
- Drink all the full bottles available, adding to the total count of bottles drunk.
15+
- Convert all the full bottles to empty bottles.
16+
- Check if the number of empty bottles is sufficient to perform an exchange. If so, exchange the empty bottles for full ones, which also increases the `numExchange` count by one.
17+
3. **Repeat**: Continue this process until there are not enough empty bottles to perform any exchanges.
18+
19+
### Data Structures:
20+
- Use simple integer variables to keep track of the number of full bottles, empty bottles, and the total count of bottles drunk. No complex data structures are needed due to the straightforward nature of the problem.
21+
22+
### Complexity:
23+
- **Time Complexity**: O(n), where n is the number of times we can drink and exchange bottles. Given the constraints (both `numBottles` and `numExchange` can go up to 100), this results in a manageable number of iterations.
24+
- **Space Complexity**: O(1), as we are only using a fixed number of variables regardless of the input size.
25+
26+
This approach effectively simulates the process of drinking and exchanging bottles, ensuring that we maximize the total number of bottles consumed while adhering to the exchange rules.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int numWaterBottles(int numBottles, int numExchange) {
3+
int totalDrunk = 0;
4+
int emptyBottles = 0;
5+
6+
while (numBottles > 0) {
7+
totalDrunk += numBottles;
8+
emptyBottles += numBottles;
9+
numBottles = emptyBottles / numExchange;
10+
emptyBottles %= numExchange;
11+
}
12+
13+
return totalDrunk;
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var numWaterBottles = function(numBottles, numExchange) {
2+
let totalDrunk = numBottles;
3+
let emptyBottles = numBottles;
4+
5+
while (emptyBottles >= numExchange) {
6+
const newBottles = Math.floor(emptyBottles / numExchange);
7+
totalDrunk += newBottles;
8+
emptyBottles = newBottles + (emptyBottles % numExchange);
9+
}
10+
11+
return totalDrunk;
12+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
3+
total_drunk = 0
4+
empty_bottles = 0
5+
6+
while numBottles > 0:
7+
total_drunk += numBottles
8+
empty_bottles += numBottles
9+
numBottles = empty_bottles // numExchange
10+
empty_bottles %= numExchange
11+
12+
return total_drunk

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,4 @@ Each problem includes:
164164
- 2025-09-29 — [Minimum Score Triangulation of Polygon](https://leetcode.com/problems/minimum-score-triangulation-of-polygon/) (Medium) → `Medium/2025-09-29-1039-Minimum-Score-Triangulation-of-Polygon`
165165
- 2025-09-30 — [Find Triangular Sum of an Array](https://leetcode.com/problems/find-triangular-sum-of-an-array/) (Medium) → `Medium/2025-09-30-2221-Find-Triangular-Sum-of-an-Array`
166166
- 2025-10-01 — [Water Bottles](https://leetcode.com/problems/water-bottles/) (Easy) → `Easy/2025-10-01-1518-Water-Bottles`
167+
- 2025-10-02 — [Water Bottles II](https://leetcode.com/problems/water-bottles-ii/) (Medium) → `Medium/2025-10-02-3100-Water-Bottles-II`

0 commit comments

Comments
 (0)