10631. Design Excel Sum Formula

Design a basic spreadsheet class that supports setting cell values, retrieving cell values, and assigning sum formulas to cells. Implement the following methods:

  • Table(int numRows, char lastCol): Constructor. Initializes a spreadsheet with numRows rows and columns from 'A' to lastCol (inclusive). All cells start with a value of 0. Row indices start at 1, and columns are labeled 'A', 'B', ..., up to lastCol.
  • void setValue(int row, char column, int value): Sets the cell at the specified row and column to value.
  • int getValue(int row, char column): Returns the value of the specified cell.
  • int setSum(int row, char column, List<String> refs): Assigns the sum of the cells or cell ranges specified in refs to the given cell, and returns the computed sum. Each string in refs can either represent a single cell (e.g., "B3") or a rectangular block of cells (e.g., "A1:C2").
Note: There will be no cyclic dependencies. e.g. if A1 depends on B1 then B1 will never depend on A1 directly or indirectly.

Examples

Table(2, "B")
// Creates a 2x2 table:
//   A B
// 1 0 0
// 2 0 0

setValue(1, "A", 3)
// Updates cell (1, "A") to 3:
//   A B
// 1 3 0
// 2 0 0

setSum(2, "B", ["A1", "A1:B1"])
// Sets (2, "B") to the sum of cell (1, "A") and the range from (1, "A") to (1, "B").
// Result: 3 + (3+0) = 6
//   A B
// 1 3 0
// 2 0 6

setValue(1, "B", 4)
// Updates (1, "B") to 4. The value at (2, "B") should also update because it has a sum formula.
//   A B
// 1 3 4
// 2 0 10

Constraints

  • 1 ≤ numRows ≤ 30
  • 'A' ≤ lastCol ≤ 'Z'
  • 1 ≤ row ≤ numRows
  • 'A' ≤ column ≤ lastCol
  • refs contains between 1 and 100 items. Each string in refs represents either a single cell (e.g., "B2") or a rectangle (e.g., "A1:C3").
  • There will be no cyclic sum dependencies between cells.




Please use Laptop/Desktop or any other large screen to add/edit code.