Problem Description
Design a simple Excel system that supports setting cell values, retrieving cell values, and defining a sum formula that can refer to individual cells and cell ranges. Once a sum formula is defined on a cell, the cell's value should automatically update if any of the cells it depends on are changed.
Key Insights
- Each cell can hold either an integer or a formula that is a weighted sum of other cells.
- A dependency graph must be maintained to know which cells depend on a given cell so that updates can propagate.
- Removing or updating a formula should clear the previous dependency links.
- Parsing of cell range strings (e.g., "A1:B2") needs to convert to the corresponding set of cell coordinates.
- Given the small grid size (max 26 rows and 26 columns), direct recomputation via DFS or recursion is efficient.
Space and Time Complexity
Time Complexity: O(R * C) in the worst-case scenario when propagating updates through all dependent formulas (with R ≤ 26, C ≤ 26). Space Complexity: O(R * C) for storing cell values, formulas, and the dependency graph.
Solution
We implement the Excel class using the following approach:
- Maintain a 2D matrix to hold the value of each cell.
- Use a hash map (or equivalent) to store formulas for cells where the formula is represented by a mapping of dependent cells (and their counts if referenced multiple times).
- Construct a dependency graph that maps a cell to the set of cells that depend on it. When a cell is updated, perform a DFS over this graph to recalculate any formulas affected by the change.
- For the sum function, parse each input string: if it represents a single cell (e.g., "A1") add that dependency; if it is a range (e.g., "A1:B2"), iterate over the range and add all cells within.
- When updating a cell's value (either via set or formula evaluation), clear any existing formula (and its dependency links) for that cell, update its value, and then propagate the change to all dependent cells recursively.
- This design avoids issues with circular dependencies (as guaranteed by the problem) and supports efficient updates given the small grid size.