|
| 1 | +--- |
| 2 | + |
| 3 | +id: water-bottles |
| 4 | +title: Water Bottles Solution |
| 5 | +sidebar_label: 1518-Water-Bottles |
| 6 | +tags: |
| 7 | + - Greedy |
| 8 | + - Simulation |
| 9 | + - LeetCode |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - C++ |
| 13 | + - JavaScript |
| 14 | +description: "This is a solution to the Water Bottles problem on LeetCode." |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +In this page, we will solve the Water Bottles problem using different approaches: iterative simulation and a more optimized approach. We will provide the implementation of the solution in Python, Java, C++, JavaScript, and more. |
| 19 | + |
| 20 | +## Problem Description |
| 21 | + |
| 22 | +There are `numBottles` water bottles that are initially full of water. You can exchange `numExchange` empty water bottles from the market with one full water bottle. |
| 23 | + |
| 24 | +The operation of drinking a full water bottle turns it into an empty bottle. |
| 25 | + |
| 26 | +Given the two integers `numBottles` and `numExchange`, return the maximum number of water bottles you can drink. |
| 27 | + |
| 28 | +### Examples |
| 29 | + |
| 30 | +**Example 1:** |
| 31 | + |
| 32 | +```plaintext |
| 33 | +Input: numBottles = 9, numExchange = 3 |
| 34 | +Output: 13 |
| 35 | +Explanation: You can exchange 3 empty bottles to get 1 full water bottle. |
| 36 | +Number of water bottles you can drink: 9 + 3 + 1 = 13. |
| 37 | +``` |
| 38 | + |
| 39 | +**Example 2:** |
| 40 | + |
| 41 | +```plaintext |
| 42 | +Input: numBottles = 15, numExchange = 4 |
| 43 | +Output: 19 |
| 44 | +Explanation: You can exchange 4 empty bottles to get 1 full water bottle. |
| 45 | +Number of water bottles you can drink: 15 + 3 + 1 = 19. |
| 46 | +``` |
| 47 | + |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- $1 <= numBottles <= 100$ |
| 51 | +- $2 <= numExchange <= 100$ |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Solution for Water Bottles Problem |
| 56 | + |
| 57 | +### Intuition and Approach |
| 58 | + |
| 59 | +The problem can be solved by simulating the process of drinking water bottles and exchanging empty ones for full ones until no more exchanges can be made. |
| 60 | + |
| 61 | +<Tabs> |
| 62 | + <tabItem value="Iterative Simulation" label="Iterative Simulation"> |
| 63 | + |
| 64 | +### Approach 1: Iterative Simulation |
| 65 | + |
| 66 | +We iteratively drink the water bottles and exchange the empty ones until no more exchanges are possible. |
| 67 | + |
| 68 | +#### Implementation |
| 69 | + |
| 70 | +```jsx live |
| 71 | +function maxBottles() { |
| 72 | + const numBottles = 9; |
| 73 | + const numExchange = 3; |
| 74 | + |
| 75 | + const maxWaterBottles = function(numBottles, numExchange) { |
| 76 | + let totalDrank = numBottles; |
| 77 | + let emptyBottles = numBottles; |
| 78 | + |
| 79 | + while (emptyBottles >= numExchange) { |
| 80 | + const newBottles = Math.floor(emptyBottles / numExchange); |
| 81 | + totalDrank += newBottles; |
| 82 | + emptyBottles = emptyBottles % numExchange + newBottles; |
| 83 | + } |
| 84 | + |
| 85 | + return totalDrank; |
| 86 | + }; |
| 87 | + |
| 88 | + const result = maxWaterBottles(numBottles, numExchange); |
| 89 | + return ( |
| 90 | + <div> |
| 91 | + <p> |
| 92 | + <b>Input:</b> numBottles = {numBottles}, numExchange = {numExchange} |
| 93 | + </p> |
| 94 | + <p> |
| 95 | + <b>Output:</b> {result} |
| 96 | + </p> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### Code in Different Languages |
| 103 | + |
| 104 | +<Tabs> |
| 105 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 106 | + <SolutionAuthor name="@manishh12"/> |
| 107 | + ```javascript |
| 108 | + function maxWaterBottles(numBottles, numExchange) { |
| 109 | + let totalDrank = numBottles; |
| 110 | + let emptyBottles = numBottles; |
| 111 | + |
| 112 | + while (emptyBottles >= numExchange) { |
| 113 | + const newBottles = Math.floor(emptyBottles / numExchange); |
| 114 | + totalDrank += newBottles; |
| 115 | + emptyBottles = emptyBottles % numExchange + newBottles; |
| 116 | + } |
| 117 | + |
| 118 | + return totalDrank; |
| 119 | + } |
| 120 | + ``` |
| 121 | + |
| 122 | + </TabItem> |
| 123 | + <TabItem value="TypeScript" label="TypeScript"> |
| 124 | + <SolutionAuthor name="@manishh12"/> |
| 125 | + ```typescript |
| 126 | + function maxWaterBottles(numBottles: number, numExchange: number): number { |
| 127 | + let totalDrank: number = numBottles; |
| 128 | + let emptyBottles: number = numBottles; |
| 129 | +
|
| 130 | + while (emptyBottles >= numExchange) { |
| 131 | + const newBottles: number = Math.floor(emptyBottles / numExchange); |
| 132 | + totalDrank += newBottles; |
| 133 | + emptyBottles = emptyBottles % numExchange + newBottles; |
| 134 | + } |
| 135 | +
|
| 136 | + return totalDrank; |
| 137 | + } |
| 138 | + ``` |
| 139 | + |
| 140 | + </TabItem> |
| 141 | + <TabItem value="Python" label="Python"> |
| 142 | + <SolutionAuthor name="@manishh12"/> |
| 143 | + ```python |
| 144 | + class Solution: |
| 145 | + def numWaterBottles(self, numBottles: int, numExchange: int) -> int: |
| 146 | + total_drank = numBottles |
| 147 | + empty_bottles = numBottles |
| 148 | +
|
| 149 | + while empty_bottles >= numExchange: |
| 150 | + new_bottles = empty_bottles // numExchange |
| 151 | + total_drank += new_bottles |
| 152 | + empty_bottles = empty_bottles % numExchange + new_bottles |
| 153 | +
|
| 154 | + return total_drank |
| 155 | + ``` |
| 156 | + |
| 157 | + </TabItem> |
| 158 | + <TabItem value="Java" label="Java"> |
| 159 | + <SolutionAuthor name="@manishh12"/> |
| 160 | + ```java |
| 161 | + class Solution { |
| 162 | + public int numWaterBottles(int numBottles, int numExchange) { |
| 163 | + int totalDrank = numBottles; |
| 164 | + int emptyBottles = numBottles; |
| 165 | +
|
| 166 | + while (emptyBottles >= numExchange) { |
| 167 | + int newBottles = emptyBottles / numExchange; |
| 168 | + totalDrank += newBottles; |
| 169 | + emptyBottles = emptyBottles % numExchange + newBottles; |
| 170 | + } |
| 171 | +
|
| 172 | + return totalDrank; |
| 173 | + } |
| 174 | + } |
| 175 | + ``` |
| 176 | + |
| 177 | + </TabItem> |
| 178 | + <TabItem value="C++" label="C++"> |
| 179 | + <SolutionAuthor name="@manishh12"/> |
| 180 | + ```cpp |
| 181 | + class Solution { |
| 182 | + public: |
| 183 | + int numWaterBottles(int numBottles, int numExchange) { |
| 184 | + int totalDrank = numBottles; |
| 185 | + int emptyBottles = numBottles; |
| 186 | +
|
| 187 | + while (emptyBottles >= numExchange) { |
| 188 | + int newBottles = emptyBottles / numExchange; |
| 189 | + totalDrank += newBottles; |
| 190 | + emptyBottles = emptyBottles % numExchange + newBottles; |
| 191 | + } |
| 192 | +
|
| 193 | + return totalDrank; |
| 194 | + } |
| 195 | + }; |
| 196 | + ``` |
| 197 | + |
| 198 | + </TabItem> |
| 199 | +</Tabs> |
| 200 | + |
| 201 | +#### Complexity Analysis |
| 202 | + |
| 203 | +- Time Complexity: $$O(\log n)$$, where `n` is the initial number of bottles, due to the iterative division. |
| 204 | +- Space Complexity: $$O(1)$$, as we are using a constant amount of extra space. |
| 205 | + |
| 206 | +</tabItem> |
| 207 | + |
| 208 | +<tabItem value="Optimized Approach" label="Optimized Approach"> |
| 209 | + |
| 210 | +### Approach 2: Optimized Approach |
| 211 | + |
| 212 | +We can derive a mathematical formula to calculate the total number of water bottles drank based on the initial number of bottles and the exchange rate. |
| 213 | + |
| 214 | +#### Implementation |
| 215 | + |
| 216 | +```jsx live |
| 217 | +function maxBottles() { |
| 218 | + const numBottles = 9; |
| 219 | + const numExchange = 3; |
| 220 | +
|
| 221 | + const maxWaterBottles = function(numBottles, numExchange) { |
| 222 | + return numBottles + Math.floor((numBottles - 1) / (numExchange - 1)); |
| 223 | + }; |
| 224 | +
|
| 225 | + const result = maxWaterBottles(numBottles, numExchange); |
| 226 | + return ( |
| 227 | + <div> |
| 228 | + <p> |
| 229 | + <b>Input:</b> numBottles = {numBottles}, numExchange = {numExchange} |
| 230 | + </p> |
| 231 | + <p> |
| 232 | + <b>Output:</b> {result} |
| 233 | + </p> |
| 234 | + </div> |
| 235 | + ); |
| 236 | +} |
| 237 | +``` |
| 238 | + |
| 239 | +#### Code in Different Languages |
| 240 | + |
| 241 | +<Tabs> |
| 242 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 243 | + <SolutionAuthor name="@manishh12"/> |
| 244 | + ```javascript |
| 245 | + function maxWaterBottles(numBottles, numExchange) { |
| 246 | + return numBottles + Math.floor((numBottles - 1) / (numExchange - 1)); |
| 247 | + } |
| 248 | + ``` |
| 249 | + |
| 250 | + </TabItem> |
| 251 | + <TabItem value="TypeScript" label="TypeScript"> |
| 252 | + <SolutionAuthor name="@manishh12"/> |
| 253 | + ```typescript |
| 254 | + function maxWaterBottles(numBottles: number, numExchange: number): number { |
| 255 | + return numBottles + Math.floor((numBottles - 1) / (numExchange - 1)); |
| 256 | + } |
| 257 | + ``` |
| 258 | + |
| 259 | + </TabItem> |
| 260 | + <TabItem value="Python" label="Python"> |
| 261 | + <SolutionAuthor name="@manishh12"/> |
| 262 | + ```python |
| 263 | + class Solution: |
| 264 | + def numWaterBottles(self, numBottles: int, numExchange: int) -> int: |
| 265 | + return numBottles + (numBottles - 1) // (numExchange - 1) |
| 266 | + ``` |
| 267 | + |
| 268 | + </TabItem> |
| 269 | + <TabItem value="Java" label="Java"> |
| 270 | + <SolutionAuthor name="@manishh12"/> |
| 271 | + ```java |
| 272 | + class Solution { |
| 273 | + public int numWaterBottles(int numBottles, int numExchange) { |
| 274 | + return numBottles + (num |
| 275 | +
|
| 276 | +Bottles - 1) / (numExchange - 1); |
| 277 | + } |
| 278 | + } |
| 279 | + ``` |
| 280 | + |
| 281 | + </TabItem> |
| 282 | + <TabItem value="C++" label="C++"> |
| 283 | + <SolutionAuthor name="@manishh12"/> |
| 284 | + ```cpp |
| 285 | + class Solution { |
| 286 | + public: |
| 287 | + int numWaterBottles(int numBottles, int numExchange) { |
| 288 | + return numBottles + (numBottles - 1) / (numExchange - 1); |
| 289 | + } |
| 290 | + }; |
| 291 | + ``` |
| 292 | + |
| 293 | + </TabItem> |
| 294 | +</Tabs> |
| 295 | + |
| 296 | +#### Complexity Analysis |
| 297 | + |
| 298 | +- Time Complexity: $$O(1)$$, as we directly calculate the result using a formula. |
| 299 | +- Space Complexity: $$O(1)$$, as we are using a constant amount of extra space. |
| 300 | + |
| 301 | +</tabItem> |
| 302 | +</Tabs> |
| 303 | + |
| 304 | +:::tip Note |
| 305 | + |
| 306 | +By using both iterative simulation and an optimized mathematical approach, we can efficiently solve the Water Bottles problem. The choice between the two approaches depends on the specific requirements and constraints of the problem. |
| 307 | + |
| 308 | +::: |
| 309 | + |
| 310 | +## References |
| 311 | + |
| 312 | +- **LeetCode Problem:** [Water Bottles](https://leetcode.com/problems/water-bottles/) |
| 313 | +- **Solution Link:** [Water Bottles Solution on LeetCode](https://leetcode.com/problems/water-bottles/solution/) |
| 314 | +- **Authors LeetCode Profile:** [Manish Kumar Gupta](https://leetcode.com/_manishh12/) |
| 315 | + |
0 commit comments