| | 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.BaseballGame; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class BaseballGameIterative : IBaseballGame |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="operations"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int CalPoints(string[] operations) |
| 2 | 24 | | { |
| 2 | 25 | | var scores = new List<int>(); |
| | 26 | |
|
| 32 | 27 | | foreach (var operation in operations) |
| 13 | 28 | | { |
| 13 | 29 | | switch (operation) |
| | 30 | | { |
| | 31 | | case "D": |
| 2 | 32 | | scores.Add(scores[^1] * 2); |
| 2 | 33 | | break; |
| | 34 | | case "C": |
| 2 | 35 | | scores.RemoveAt(scores.Count - 1); |
| 2 | 36 | | break; |
| | 37 | | case "+": |
| 3 | 38 | | scores.Add(scores[^1] + scores[^2]); |
| 3 | 39 | | break; |
| | 40 | | default: |
| 6 | 41 | | scores.Add(int.Parse(operation)); |
| 6 | 42 | | break; |
| | 43 | | } |
| 13 | 44 | | } |
| | 45 | |
|
| 2 | 46 | | return scores.Sum(); |
| 2 | 47 | | } |
| | 48 | | } |