Skip to content

Commit 228c567

Browse files
solutions and notes for problem 1518
1 parent 7552ee6 commit 228c567

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

problems/1518/paxtonfitzpatrick.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,53 @@
22

33
## Initial thoughts (stream-of-consciousness)
44

5+
- seems pretty simple...
6+
- when exchanging empties for fulls, will need to use floor division to figure out how many fulls we can get and also modulo to figure out how many empties couldn't be echanged
7+
- also need to keep track of unexchanged empties in case we can accumulate enough for another full (`numExchange`)
8+
- stop when we have no more fulls and not enough empties to exchange for a full
9+
- I think I'll structure this as a `while` loop where each time through the loop we can drink fulls and/or exchange empties
10+
- There's probably a more efficient way to do this without looping, where we calculate everything from the initial values (so, $O(1)$), but this seems like an easier place to start.
11+
- I bet there's also a way to set this up recursively, which could be interesting, but unlikely to be optimal
12+
513
## Refining the problem, round 2 thoughts
614

15+
- Version 2: I noticed some small tweaks I could make to the logic that I think would speed up the `while` loop version slightly
16+
- note: key to not having to modify `n_empties` twice each iteration is updating `n_fulls` and `n_empties` simultaneously. New values for both variables depend on the old value of the other, so updating both on the same line ensures both calculations use the "old" values. Could also have stored `n_empties + n_fulls` in a temporary variable, but that would've taken a teeny bit more time & memory.
17+
718
## Attempted solution(s)
19+
20+
Version 1:
21+
22+
```python
23+
class Solution:
24+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
25+
n_fulls = numBottles
26+
n_empties = 0
27+
n_drank = 0
28+
while True:
29+
n_drank += n_fulls
30+
n_empties += n_fulls
31+
if n_empties < numExchange:
32+
return n_drank
33+
n_fulls = n_empties // numExchange
34+
n_empties = n_empties % numExchange
35+
```
36+
37+
![Version 1 results](https://github.com/paxtonfitzpatrick/leetcode-solutions/assets/26118297/08d502ae-a947-49fc-89a9-fb578ea937a6)
38+
39+
Version 2:
40+
841
```python
9-
class Solution: # paste your code here!
10-
...
42+
class Solution:
43+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
44+
n_drank = numBottles
45+
n_fulls = numBottles // numExchange
46+
n_empties = numBottles % numExchange
47+
while n_fulls > 0:
48+
n_drank += n_fulls
49+
n_fulls, n_empties = (n_empties + n_fulls) // numExchange, (n_empties + n_fulls) % numExchange
50+
return n_drank
1151
```
52+
53+
![Version 2 results](https://github.com/paxtonfitzpatrick/leetcode-solutions/assets/26118297/ba0ca272-8761-4cc5-9c2f-9deb12d40361)
54+
Appears to be slower than version 1, but probably just due to random variation runtime of the test cases. Pretty confident that in reality, version 2 is marginally faster.

0 commit comments

Comments
 (0)