| | | 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.DesignSpreadsheet; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class DesignSpreadsheetMatrix : IDesignSpreadsheet |
| | | 16 | | { |
| | | 17 | | private const int Columns = 'Z' - 'A' + 1; |
| | | 18 | | |
| | | 19 | | private readonly int[,] _spreadsheet; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Time complexity - O(n), where n is rows |
| | | 23 | | /// Space complexity - O(n), where n is rows |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="rows"></param> |
| | 1 | 26 | | public DesignSpreadsheetMatrix(int rows) |
| | 1 | 27 | | { |
| | 1 | 28 | | _spreadsheet = new int[rows, Columns]; |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Time complexity - O(m), where m is the cell length |
| | | 33 | | /// Space complexity - O(1) |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="cell"></param> |
| | | 36 | | /// <param name="value"></param> |
| | | 37 | | public void SetCell(string cell, int value) |
| | 3 | 38 | | { |
| | 3 | 39 | | var (row, column) = ParseCell(cell, 0, cell.Length); |
| | | 40 | | |
| | 3 | 41 | | _spreadsheet[row, column] = value; |
| | 3 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Time complexity - O(m), where m is the cell length |
| | | 46 | | /// Space complexity - O(1) |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="cell"></param> |
| | | 49 | | public void ResetCell(string cell) |
| | 1 | 50 | | { |
| | 1 | 51 | | SetCell(cell, 0); |
| | 1 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Time complexity - O(m), where m is the formula length |
| | | 56 | | /// Space complexity - O(1) |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="formula"></param> |
| | | 59 | | /// <returns></returns> |
| | | 60 | | public int GetValue(string formula) |
| | 4 | 61 | | { |
| | 4 | 62 | | var indexOfPlus = formula.IndexOf('+'); |
| | | 63 | | |
| | 4 | 64 | | var left = ParseToken(formula, 1, indexOfPlus); |
| | | 65 | | |
| | 4 | 66 | | var right = ParseToken(formula, indexOfPlus + 1, formula.Length); |
| | | 67 | | |
| | 4 | 68 | | return left + right; |
| | 4 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Time complexity - O(m), where m is the s length |
| | | 73 | | /// Space complexity - O(1) |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="s"></param> |
| | | 76 | | /// <param name="start"></param> |
| | | 77 | | /// <param name="end"></param> |
| | | 78 | | /// <returns></returns> |
| | | 79 | | private int ParseToken(string s, int start, int end) |
| | 8 | 80 | | { |
| | 8 | 81 | | if (char.IsDigit(s[start])) |
| | 3 | 82 | | { |
| | 3 | 83 | | return ParseValue(s, start, end); |
| | | 84 | | } |
| | | 85 | | |
| | 5 | 86 | | var (row, column) = ParseCell(s, start, end); |
| | | 87 | | |
| | 5 | 88 | | return _spreadsheet[row, column]; |
| | 8 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Time complexity - O(m), where m is the length of the s string |
| | | 93 | | /// Space complexity - O(1) |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="s"></param> |
| | | 96 | | /// <param name="start"></param> |
| | | 97 | | /// <param name="end"></param> |
| | | 98 | | /// <returns></returns> |
| | | 99 | | private static (int Row, int Column) ParseCell(string s, int start, int end) |
| | 8 | 100 | | { |
| | 8 | 101 | | var column = s[start] - 'A'; |
| | | 102 | | |
| | 8 | 103 | | var row = ParseValue(s, start + 1, end) - 1; |
| | | 104 | | |
| | 8 | 105 | | return (row, column); |
| | 8 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Time complexity - O(m), where m is the s length |
| | | 110 | | /// Space complexity - O(1) |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="s"></param> |
| | | 113 | | /// <param name="start"></param> |
| | | 114 | | /// <param name="end"></param> |
| | | 115 | | /// <returns></returns> |
| | | 116 | | private static int ParseValue(string s, int start, int end) |
| | 11 | 117 | | { |
| | 11 | 118 | | var value = 0; |
| | | 119 | | |
| | 44 | 120 | | for (var i = start; i < end; i++) |
| | 11 | 121 | | { |
| | 11 | 122 | | var c = s[i]; |
| | | 123 | | |
| | 11 | 124 | | value = (value * 10) + (c - '0'); |
| | 11 | 125 | | } |
| | | 126 | | |
| | 11 | 127 | | return value; |
| | 11 | 128 | | } |
| | | 129 | | } |