Skip to content

Commit 60200d3

Browse files
author
Sanajit Jana
committed
Add: 1518. Water Bottles
1 parent b210a29 commit 60200d3

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

TEMPLATE.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# [<Question Title>](<Question Link>)
2+
3+
## Question Description
4+
<Write the problem description in your own words or copy it from LeetCode. Keep it concise but clear.>
5+
6+
---
7+
8+
## Constraints
9+
- <Constraint 1>
10+
- <Constraint 2>
11+
- <Constraint 3>
12+
(Write the input limits, data ranges, and any conditions from the problem statement)
13+
14+
---
15+
16+
## Approach
17+
<Explain your step-by-step thought process.
18+
Why this approach works.
19+
Mention if you considered alternatives and why this one is better.>
20+
21+
---
22+
23+
## Dry Run
24+
Example Input: `<example input>`
25+
26+
Step-by-step execution:
27+
- Step 1: <explain>
28+
- Step 2: <explain>
29+
- Step 3: <explain>
30+
31+
Final Answer = `<result>`
32+
33+
---
34+
35+
## Solution
36+
```java
37+
class Solution {
38+
public <returnType> <methodName>(<parameters>) {
39+
// your solution code here
40+
}
41+
}
42+
```
43+
44+
---
45+
46+
## Time and Space Complexity
47+
- **Time Complexity:** O(<time analysis>)
48+
- **Space Complexity:** O(<space analysis>)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# [1518. Water Bottles](https://leetcode.com/problems/water-bottles/?envType=daily-question&envId=2025-10-02)
2+
3+
## Question Description
4+
There are numBottles water bottles that are initially full of water.
5+
You can exchange numExchange empty water bottles from the market with one full water bottle.
6+
7+
The operation of drinking a full water bottle turns it into an empty bottle.
8+
9+
Given the two integers numBottles and numExchange, return the maximum number of water bottles you can drink.
10+
11+
---
12+
13+
## Constraints
14+
- 1 <= numBottles <= 100
15+
- 2 <= numExchange <= 100
16+
17+
---
18+
19+
## Approach
20+
We simulate the process step by step:
21+
1. Drink all current bottles and increase the total count.
22+
2. Track how many empty bottles are collected.
23+
3. If empty bottles are enough for an exchange, convert them into new full bottles.
24+
4. Continue until you cannot exchange anymore.
25+
26+
This works because each exchange is independent and only depends on the count of empty bottles at each step.
27+
28+
---
29+
30+
## Dry Run
31+
Example Input: numBottles = 9, numExchange = 3
32+
33+
Step-by-step execution:
34+
- Start: count = 0, empty = 0, numBottles = 9
35+
- Drink 9 bottles → count = 9, empty = 9
36+
- Exchange 9/3 = 3 → numBottles = 3, empty = 0
37+
- Drink 3 bottles → count = 12, empty = 3
38+
- Exchange 3/3 = 1 → numBottles = 1, empty = 0
39+
- Drink 1 bottle → count = 13, empty = 1
40+
- Not enough bottles to exchange → Stop
41+
42+
Final Answer = 13
43+
44+
---
45+
46+
## Solution
47+
```java
48+
class Solution {
49+
public int numWaterBottles(int numBottles, int numExchange) {
50+
int count = 0;
51+
int empty = 0;
52+
while (numBottles > 0) {
53+
count += numBottles;
54+
empty += numBottles;
55+
56+
numBottles = empty / numExchange;
57+
empty = empty % numExchange;
58+
}
59+
return count;
60+
}
61+
}
62+
```
63+
64+
---
65+
66+
## Time and Space Complexity
67+
- **Time Complexity:** O(numBottles) in the worst case, as we simulate exchanges until bottles run out.
68+
- **Space Complexity:** O(1), using only a few variables.

0 commit comments

Comments
 (0)