| | | 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.SimpleBankSystem; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class SimpleBankSystemLookup : ISimpleBankSystem |
| | | 16 | | { |
| | | 17 | | private readonly long[] _balance; |
| | | 18 | | private readonly int _balanceLength; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Time complexity - O(1) |
| | | 22 | | /// Space complexity - O(n) |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="balance"></param> |
| | 1 | 25 | | public SimpleBankSystemLookup(long[] balance) |
| | 1 | 26 | | { |
| | 1 | 27 | | _balance = balance; |
| | 1 | 28 | | _balanceLength = balance.Length; |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Time complexity - O(1) |
| | | 33 | | /// Space complexity - O(1) |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="account1"></param> |
| | | 36 | | /// <param name="account2"></param> |
| | | 37 | | /// <param name="money"></param> |
| | | 38 | | /// <returns></returns> |
| | | 39 | | public bool Transfer(int account1, int account2, long money) |
| | 2 | 40 | | { |
| | 2 | 41 | | if (account1 > _balanceLength || account2 > _balanceLength) |
| | 0 | 42 | | { |
| | 0 | 43 | | return false; |
| | | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | var balance1Index = account1 - 1; |
| | 2 | 47 | | var balance2Index = account2 - 1; |
| | | 48 | | |
| | 2 | 49 | | if (_balance[balance1Index] < money) |
| | 1 | 50 | | { |
| | 1 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | _balance[balance1Index] -= money; |
| | 1 | 55 | | _balance[balance2Index] += money; |
| | | 56 | | |
| | 1 | 57 | | return true; |
| | 2 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Time complexity - O(1) |
| | | 62 | | /// Space complexity - O(1) |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="account"></param> |
| | | 65 | | /// <param name="money"></param> |
| | | 66 | | /// <returns></returns> |
| | | 67 | | public bool Deposit(int account, long money) |
| | 1 | 68 | | { |
| | 1 | 69 | | if (account > _balanceLength) |
| | 0 | 70 | | { |
| | 0 | 71 | | return false; |
| | | 72 | | } |
| | | 73 | | |
| | 1 | 74 | | var balanceIndex = account - 1; |
| | | 75 | | |
| | 1 | 76 | | _balance[balanceIndex] += money; |
| | | 77 | | |
| | 1 | 78 | | return true; |
| | 1 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Time complexity - O(1) |
| | | 83 | | /// Space complexity - O(1) |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="account"></param> |
| | | 86 | | /// <param name="money"></param> |
| | | 87 | | /// <returns></returns> |
| | | 88 | | public bool Withdraw(int account, long money) |
| | 2 | 89 | | { |
| | 2 | 90 | | if (account > _balanceLength) |
| | 1 | 91 | | { |
| | 1 | 92 | | return false; |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | var balanceIndex = account - 1; |
| | | 96 | | |
| | 1 | 97 | | if (_balance[balanceIndex] < money) |
| | 0 | 98 | | { |
| | 0 | 99 | | return false; |
| | | 100 | | } |
| | | 101 | | |
| | 1 | 102 | | _balance[balanceIndex] -= money; |
| | | 103 | | |
| | 1 | 104 | | return true; |
| | 2 | 105 | | } |
| | | 106 | | } |