Problem · Graph
Evaluate Live Spreadsheet Expressions
Learn this problemProblem statement
Implement a spreadsheet whose cells are named by uppercase letters followed by a positive row number, such as A1 or BC12. Process an ordered array of operations.
SET cell expressionoverwrites the cell. An expression is one or more terms joined by+. Each term is either an integer literal, which may be negative, or another cell name.GET cellevaluates the cell using the spreadsheet's current contents and appends its value to the result.
References are live: after a referenced cell is overwritten, later reads use the new value through every dependency level. Every cell reached by a GET has been assigned, and the current dependency graph is acyclic.
Return the signed 64-bit results of all GET operations in order.
Function
runSpreadsheet(operations: String[]) → long[]Examples
Example 1
operations = ["SET A1 5","SET B1 A1+3","GET B1","SET A1 10","GET B1"]return = [8,13]B1 keeps a live reference to A1, so the second read observes the overwrite.
Example 2
operations = ["SET X1 -4","SET Y1 X1+10+X1","GET Y1","SET X1 3","GET Y1"]return = [2,16]Repeated cell references count separately, and negative literals are supported.
Constraints
1 <= operations.length <= 100000.- Each cell name matches
[A-Z]+[1-9][0-9]*. - Every expression contains at least one term. Terms contain no whitespace and are separated by one
+. - The total number of expression terms across all
SEToperations is at most100000. - Across all
GEToperations, the sum of stored terms in the distinct cells reachable by each query is at most200000. - Every cell reached by a
GEThas been assigned, and every represented dependency graph is acyclic. - Every literal, intermediate sum, and result fits in a signed 64-bit integer.