Advertisement

Design Spreadsheet - LeetCode 3484 Solution

Design Spreadsheet - Complete Solution Guide

Design Spreadsheet is LeetCode problem 3484, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A spreadsheet is a grid with 26 columns (labeled from 'A' to 'Z' ) and a given number of rows . Each cell in the spreadsheet can hold an integer value between 0 and 10 5 . Implement the Spreadsheet class: Spreadsheet(int rows) Initializes a spreadsheet with 26 columns (labeled 'A' to 'Z' ) and the specified number of rows. All cells are initially set to 0. void setCell(String cell, int value) Sets the value of the specified cell . The cell reference is provided in the format "AX" (e.g., "A1" , "

Detailed Explanation

The "Design Spreadsheet" problem requires implementing a `Spreadsheet` class that simulates basic spreadsheet functionality. The spreadsheet has a fixed number of rows and 26 columns (A-Z). The class should allow setting cell values, resetting cells to zero, and evaluating simple formulas of the form "=X+Y", where X and Y can be either cell references (e.g., "A1") or non-negative integers. The problem constraints specify the range of row numbers, cell values, and the format of formulas. A key element is that if a cell is referenced in a formula before it has been assigned a value via `setCell`, its value is assumed to be 0.

Solution Approach

The provided code implements the `Spreadsheet` class with a 2D array (`grid`) to store cell values. The `setCell` method updates the grid with the given value. The `resetCell` method sets the cell value to 0 using `setCell`. The `getValue` method parses the formula, extracts the two terms (X and Y), and evaluates each term either by retrieving the value from the grid if it's a cell reference or by parsing it as an integer. Finally, it returns the sum of the evaluated terms.

Step-by-Step Algorithm

  1. Step 1: **Initialization:** The `Spreadsheet` constructor creates a 2D integer array (grid) of size `rows x 26` and initializes all cells to 0.
  2. Step 2: **`setCell(cell, value)`:** Parses the `cell` string to extract the row and column indices. Updates the corresponding cell in the `grid` with the given `value`.
  3. Step 3: **`resetCell(cell)`:** Calls `setCell` with the given `cell` and a `value` of 0.
  4. Step 4: **`getValue(formula)`:** Removes the '=' sign from the beginning of the formula. Splits the formula string by the '+' character into two terms.
  5. Step 5: **`_evaluate_term(term)`:** Checks if the term is a cell reference (starts with a letter A-Z) or an integer. If it's a cell reference, parses the indices, retrieves the value from the `grid`, and returns it. If it's an integer, parses it and returns it.
  6. Step 6: **`getValue(formula)` (continued):** Evaluates both terms using `_evaluate_term`. Returns the sum of the two evaluated terms.

Key Insights

  • Insight 1: Efficient cell addressing is crucial. Converting cell names (e.g., "A1") to row and column indices is a common operation and must be handled correctly.
  • Insight 2: The limited formula structure (=X+Y) simplifies parsing. We don't need a full expression parser; splitting the string by '+' and then checking if each part is a number or cell reference is sufficient.
  • Insight 3: The initial state of cells matters. All cells start with a value of 0, and any read before setting a value should return 0.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, String, Design, Matrix.

Companies

Asked at: Rippling.