Skip to content

Commit 7c6a459

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

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Water Bottles (Easy)
2+
3+
**Problem ID:** 1518
4+
**Date:** 2025-10-01
5+
**Link:** https://leetcode.com/problems/water-bottles/
6+
7+
## Approach
8+
9+
To solve the "Water Bottles" problem, we can use a straightforward iterative approach to track the total number of water bottles consumed. The main idea is to continuously drink from the available full bottles and exchange the resulting empty bottles for new full ones until no more exchanges can be made.
10+
11+
### Approach:
12+
13+
1. **Initialization**: Start with the total number of full bottles (`numBottles`) and initialize a counter for the total number of bottles consumed.
14+
15+
2. **Iterative Drinking and Exchanging**:
16+
- While there are full bottles available:
17+
- Drink all the full bottles, adding this number to the total consumed count.
18+
- Calculate the number of empty bottles generated from drinking.
19+
- Determine how many new full bottles can be obtained by exchanging the empty bottles. This is done by integer division of the number of empty bottles by `numExchange`.
20+
- Update the count of empty bottles to reflect those exchanged and those remaining after the exchange.
21+
22+
3. **Termination**: The process continues until the number of empty bottles is less than `numExchange`, meaning no further exchanges can be made.
23+
24+
### Data Structures:
25+
- We primarily use integer variables to keep track of the number of full bottles, empty bottles, and the total consumed count. No complex data structures are required due to the simplicity of the problem.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(log(numBottles)), where the logarithmic factor arises because the number of full bottles decreases significantly with each round of exchanges and drinking.
29+
- **Space Complexity**: O(1), as we only use a fixed number of variables regardless of the input size.
30+
31+
This approach efficiently calculates the maximum number of water bottles that can be consumed through a simple loop, ensuring clarity and directness in the solution.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int numWaterBottles(int numBottles, int numExchange) {
3+
int totalDrunk = numBottles;
4+
int emptyBottles = numBottles;
5+
6+
while (emptyBottles >= numExchange) {
7+
int newBottles = emptyBottles / numExchange;
8+
totalDrunk += newBottles;
9+
emptyBottles = newBottles + (emptyBottles % numExchange);
10+
}
11+
12+
return totalDrunk;
13+
}
14+
}
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+
let newBottles = Math.floor(emptyBottles / numExchange);
7+
totalDrunk += newBottles;
8+
emptyBottles = newBottles + (emptyBottles % numExchange);
9+
}
10+
11+
return totalDrunk;
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
3+
total_drunk = numBottles
4+
empty_bottles = numBottles
5+
6+
while empty_bottles >= numExchange:
7+
new_bottles = empty_bottles // numExchange
8+
total_drunk += new_bottles
9+
empty_bottles = new_bottles + (empty_bottles % numExchange)
10+
11+
return total_drunk

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,4 @@ Each problem includes:
163163
- 2025-09-28 — [Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/) (Easy) → `Easy/2025-09-28-976-Largest-Perimeter-Triangle`
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`
166+
- 2025-10-01 — [Water Bottles](https://leetcode.com/problems/water-bottles/) (Easy) → `Easy/2025-10-01-1518-Water-Bottles`

0 commit comments

Comments
 (0)