LeetCode Entry
3100. Water Bottles II
Total drinks with growing exchange rate empty for full
3100. Water Bottles II medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1130
Problem TLDR
Total drinks with growing exchange rate empty for full #medium #simulation
Intuition
Simulate the process. Don’t forget to keep lefover empty bottles.
Approach
- there is a O(1) math solution exists
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 103ms
fun maxBottlesDrunk(b: Int, x: Int, e: Int = 0): Int =
b + if (b+e<x) 0 else maxBottlesDrunk(1,x+1,b+e-x)
// 3ms
pub fn max_bottles_drunk(mut b: i32, mut x: i32) -> i32 {
let (mut e, mut d) = (0, 0);
while b > 0 || e >= x {
d += b; e += b; b = if (e < x) {0} else {e-=x;x+=1;1};
} d
}