| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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.LemonadeChange; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class LemonadeChangeGreedy : ILemonadeChange |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="bills"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public bool LemonadeChange(int[] bills) |
| 5 | 24 | | { |
| 5 | 25 | | var fiveBills = 0; |
| 5 | 26 | | var tenBills = 0; |
| | 27 | |
|
| 77 | 28 | | foreach (var bill in bills) |
| 33 | 29 | | { |
| 33 | 30 | | switch (bill) |
| | 31 | | { |
| | 32 | | case 5: |
| 15 | 33 | | fiveBills++; |
| | 34 | |
|
| 15 | 35 | | break; |
| 10 | 36 | | case 10 when fiveBills > 0: |
| 9 | 37 | | fiveBills--; |
| 9 | 38 | | tenBills++; |
| | 39 | |
|
| 9 | 40 | | break; |
| | 41 | | case 10: |
| 1 | 42 | | return false; |
| | 43 | | case 20: |
| 8 | 44 | | switch (fiveBills) |
| | 45 | | { |
| 5 | 46 | | case > 0 when tenBills > 0: |
| 5 | 47 | | fiveBills--; |
| 5 | 48 | | tenBills--; |
| 5 | 49 | | break; |
| | 50 | | case > 2: |
| 0 | 51 | | fiveBills -= 3; |
| 0 | 52 | | break; |
| | 53 | | default: |
| 3 | 54 | | return false; |
| | 55 | | } |
| | 56 | |
|
| 5 | 57 | | break; |
| | 58 | | } |
| 29 | 59 | | } |
| | 60 | |
|
| 1 | 61 | | return true; |
| 5 | 62 | | } |
| | 63 | | } |