405. Excel Cell Values and Sum Formulas
Excel Cell Values and Sum Formulas

Design a basic Excel spreadsheet that supports storing integer values, retrieving cell values, and assigning sum formulas to cells.

Rows are numbered starting from 1, and columns are labeled from 'A' through the configured final column. Every cell initially contains 0.

Class

Excel

Constructor

Excel

Excel(int H, char W)

Parameters

  • H: The number of rows in the spreadsheet.
  • W: The final column label. The spreadsheet contains every column from 'A' through W.

Initialize an H-row spreadsheet whose cells all contain 0.

Methods

Set

void Set(int row, char column, int val)

Parameters

  • row: The row number of the cell.
  • column: The column label of the cell.
  • val: The new integer value.

Assign val to the specified cell. If that cell previously contained a sum formula, the formula is removed and replaced by the given value.

Get

int Get(int row, char column)

Parameters

  • row: The row number of the requested cell.
  • column: The column label of the requested cell.

Returns

Return the current value of the specified cell. If the cell contains a formula, return its value after applying all relevant cell updates.

Sum

int Sum(int row, char column, List<String> numbers)

Parameters

  • row: The row number of the formula cell.
  • column: The column label of the formula cell.
  • numbers: Cell references and rectangular cell ranges whose values must be added.

Returns

Store a sum formula in the specified cell and return its current result. This formula replaces any value or formula previously stored in that cell.

The formula remains active until the cell is overwritten by another call to Set or Sum. Its value must update when any directly or indirectly referenced cell changes.

Cell Reference Format

  • A single cell is written as "ColRow". For example, "D4" represents column D, row 4.
  • A rectangular range is written as "TopLeft:BottomRight". For example, "B2:D5" contains every cell from B2 through D5, inclusive.

Each occurrence of a cell contributes separately to the result. Therefore, cells repeated directly, through overlapping ranges, or through duplicate entries are counted multiple times.

Constraints

  • 1 ≤ H ≤ 26
  • 'A' ≤ W ≤ 'Z'
  • 1 ≤ row ≤ H
  • 'A' ≤ column ≤ W
  • -100 ≤ val ≤ 100
  • 1 ≤ numbers.size() ≤ 100
  • Every entry in numbers is a valid cell reference or rectangular range within the spreadsheet.
  • The first cell of a range is its top-left corner, and the second cell is its bottom-right corner.
  • Sum formulas never create a direct or indirect circular dependency.
  • At most 1,000 calls are made to Set, Get, and Sum.
  • Every returned value fits in a 32-bit signed integer.
  • Each Excel instance is independent and begins with all cells set to 0.

Examples

Example 1

Excel(H = 4, W = "D")

Creates a spreadsheet with four rows and columns A through D. Every cell starts at 0.

Set(row = 2, column = "B", val = 5)

Output: No return value. Cell B2 now contains 5.

Sum(row = 4, column = "D", numbers = ["B2", "A1:B2"])

Output: 10

Cell B2 is counted once by "B2" and once inside "A1:B2".

Set(row = 1, column = "A", val = 3)

Output: No return value. The formula stored in D4 automatically changes from 10 to 13.

Get(row = 4, column = "D")

Output: 13

Example 2

Excel(H = 3, W = "C")

Creates a three-row spreadsheet with columns A, B, and C.

Set(row = 1, column = "A", val = 4)

Output: No return value.

Sum(row = 3, column = "C", numbers = ["A1:B2"])

Output: 4

Set(row = 3, column = "C", val = 7)

Output: No return value. The formula in C3 is removed.

Set(row = 2, column = "B", val = 9)

Output: No return value. Since the formula in C3 was overwritten, changing B2 does not change C3.

Get(row = 3, column = "C")

Output: 7



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