Skip to content

Commit a413106

Browse files
found an analytic solution to 1518!
this took way longer than i had hoped, but was kind of fun
1 parent 7ebade2 commit a413106

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

problems/1518/jeremymanning.md

+55
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,58 @@ class Solution:
4343
![Screenshot 2024-07-06 at 11 47 50 PM](https://github.com/ContextLab/leetcode-solutions/assets/9030494/66a9540c-5afc-4cd4-9652-6e8aa99ab59d)
4444

4545
Slow, but I'll take it-- solved!
46+
47+
---
48+
49+
# Thinking more about this...
50+
51+
- Let's see if we can come up with an analytic solution
52+
- We can initially drink `numBottles` (let's write this as $B$ to simplify notation)
53+
- After that, every `numExchange` bottles (we'll write this as $E$ to simplify notation) turns into a full bottle (which can be added to the total) and then an empty bottle (which can be exchanged in the next round)
54+
55+
So the total drinkable number of bottles can be given by the series: $\text{total} = B + \left\lfloor \frac{B}{E} \right\rfloor + \left\lfloor \frac{\left\lfloor \frac{B}{E} \right\rfloor + (B \mod E)}{E} \right\rfloor + \left\lfloor \frac{\left\lfloor \frac{\left\lfloor \frac{B}{E} \right\rfloor + (B \mod E)}{E} \right\rfloor + ((B \mod E) \mod E)}{E} \right\rfloor + \ldots$
56+
57+
In other words:
58+
- Start by drinking $B$ bottles, which yields $B$ empty bottles
59+
- Those empties can be exchanged for $\left\lfloor \frac{B}{E} \right\rfloor$ new full bottles
60+
- There are $B \mod E$ additional empty bottles left after that exchange
61+
- In each subsequent round, we can repeat this same process (divide by $E$, take the floor, add this to the total number of drinks, add the remainder to the total number of empties)
62+
63+
Interestingly, we can see that $\left\lfloor \frac{\left\lfloor \frac{B}{E} \right\rfloor + (B \mod E)}{E} \right\rfloor$ simplifies to $\left\lfloor \frac{\left\lfloor \frac{B}{E} \right\rfloor}{E} \right\rfloor$:
64+
- $B \mod E$ is always less than $E$
65+
- Therefore $(B \mod E) / E$ is always less than 1, which means we can get rid of it in the "floor" operation
66+
67+
So now we have $\left\lfloor \frac{\left\lfloor \frac{B}{E} \right\rfloor}{E} \right\rfloor$, which simplifies to $\left\lfloor \frac{B}{E^2} \right\rfloor$. In general (as the number of exchanges approaches infinity) we can see that
68+
the exponent in the demoninator will keep growing (equal to the number of exchanges): $\sum_{k = 1}^\infty \left\lfloor \frac{B}{E^k} \right\rfloor$. The demonitor is increasing exponentially, so the series will converge to...something. I wish
69+
I remembered my calculus better 🙃.
70+
71+
Doing some Googling, it looks like the series $\sum_{k = 0}^\infty x r^k$ converges to $\frac{x}{1 - r}$ if $|r| < 1$. So...let's see...
72+
73+
We can take $x = B$ and $r = \frac{1}{E}$ (which is less than 1, since we know that $E \geq 2$). So the series will converge to
74+
75+
$\frac{B}{1 - \frac{1}{E}} = $B \frac{1}{1 - \frac{1}{E}} = B \frac{E}{E - 1}$.
76+
77+
Now we can split the fraction:
78+
79+
$B\frac{E}{E - 1} = B \left( 1 + \frac{1}{E - 1} \right)$
80+
81+
Which simplifies to:
82+
83+
$B \left( 1 + \frac{1}{E - 1} \right) = B + \frac{B}{E - 1}$. Since we can only exchange "whole" bottles, we need to round down to the nearest integer:
84+
85+
$\frac{BE}{E - 1} \approx \left\lfloor \frac{B -1}{E - 1} \right\rfloor$. So the *total* number of drinks is $B + \left\lfloor \frac{B -1}{E - 1} \right\rfloor$.
86+
87+
Finally(!!), we can turn this back into Python code:
88+
```python
89+
class Solution:
90+
def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
91+
return numBottles + (numBottles - 1) // (numExchange - 1)
92+
```
93+
- Test cases pass!!
94+
- Checking a random additional test case: `numBottles = 48, numExchange = 7` (passes!)
95+
- I'm just gonna submit this instead of checking in more detail...
96+
97+
![Screenshot 2024-07-07 at 2 47 33 PM](https://github.com/ContextLab/leetcode-solutions/assets/9030494/b17fec58-14a2-4faa-9f22-0bf1cdc13455)
98+
99+
Woo! Go math 🎉!
100+

0 commit comments

Comments
 (0)