| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.WaterBottles2; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class WaterBottles2Simulation : IWaterBottles2 |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n), where n is the number of initial bottles |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="numBottles"></param> |
| | | 22 | | /// <param name="numExchange"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int MaxBottlesDrunk(int numBottles, int numExchange) |
| | 2 | 25 | | { |
| | 2 | 26 | | var drunkBottles = 0; |
| | | 27 | | |
| | 2 | 28 | | var emptyBottles = 0; |
| | | 29 | | |
| | 7 | 30 | | while (numBottles > 0) |
| | 5 | 31 | | { |
| | 5 | 32 | | drunkBottles += numBottles; |
| | 5 | 33 | | emptyBottles += numBottles; |
| | | 34 | | |
| | 5 | 35 | | numBottles = 0; |
| | | 36 | | |
| | 10 | 37 | | while (emptyBottles >= numExchange) |
| | 5 | 38 | | { |
| | 5 | 39 | | emptyBottles -= numExchange; |
| | | 40 | | |
| | 5 | 41 | | numBottles++; |
| | 5 | 42 | | numExchange++; |
| | 5 | 43 | | } |
| | 5 | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | return drunkBottles; |
| | 2 | 47 | | } |
| | | 48 | | } |