| | 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.FinalValueOfVariableAfterPerformingOperations; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FinalValueOfVariableAfterPerformingOperationsDictionary : IFinalValueOfVariableAfterPerformingOperations |
| | 16 | | { |
| 3 | 17 | | private readonly Dictionary<string, int> _operationsDictionary = new() |
| 3 | 18 | | { |
| 3 | 19 | | { "++X", 1 }, { "X++", 1 }, { "--X", -1 }, { "X--", -1 } |
| 3 | 20 | | }; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Time complexity - O(n) |
| | 24 | | /// Space complexity - O(1) |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="operations"></param> |
| | 27 | | /// <returns></returns> |
| | 28 | | public int FinalValueAfterOperations(string[] operations) |
| 3 | 29 | | { |
| 3 | 30 | | var finalValue = 0; |
| | 31 | |
|
| 29 | 32 | | foreach (var operation in operations) |
| 10 | 33 | | { |
| 10 | 34 | | _operationsDictionary.TryGetValue(operation, out var value); |
| | 35 | |
|
| 10 | 36 | | finalValue += value; |
| 10 | 37 | | } |
| | 38 | |
|
| 3 | 39 | | return finalValue; |
| 3 | 40 | | } |
| | 41 | | } |