You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: problems/1518/jeremymanning.md
+55
Original file line number
Diff line number
Diff line change
@@ -43,3 +43,58 @@ class Solution:
43
43

44
44
45
45
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
$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:
0 commit comments