Skip to content

Commit 41a484b

Browse files
chore: add LeetCode daily solution
1 parent a31e3f7 commit 41a484b

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Design Spreadsheet (Medium)
2+
3+
**Problem ID:** 3484
4+
**Date:** 2025-09-19
5+
**Link:** https://leetcode.com/problems/design-spreadsheet/
6+
7+
## Approach
8+
9+
To solve the "Design Spreadsheet" problem, we can adopt an object-oriented approach by implementing a `Spreadsheet` class that encapsulates the required functionalities. Here's a concise breakdown of the approach:
10+
11+
### Main Idea:
12+
The goal is to create a spreadsheet that supports setting and resetting cell values, along with evaluating simple arithmetic formulas involving cell references and integers.
13+
14+
### Data Structures:
15+
1. **2D Array (or List of Lists)**: To represent the spreadsheet, we can use a 2D list where each element corresponds to a cell in the spreadsheet. The rows will be indexed from 0 to `rows - 1`, and the columns will be represented by indices corresponding to letters 'A' to 'Z' (0 to 25).
16+
17+
2. **Dictionary (or HashMap)**: To facilitate quick access to cell values, we can maintain a dictionary that maps cell references (like "A1", "B2") to their respective values. This will allow us to handle cases where cells have not been explicitly set, defaulting to 0.
18+
19+
### Methods:
20+
1. **Constructor (`Spreadsheet(int rows)`)**: Initializes the spreadsheet with the specified number of rows and sets all cell values to 0.
21+
22+
2. **`setCell(String cell, int value)`**: Converts the cell reference into its respective row and column indices, updates the value in the 2D list, and also updates the dictionary.
23+
24+
3. **`resetCell(String cell)`**: Similar to `setCell`, but sets the specified cell's value back to 0 in both the 2D list and the dictionary.
25+
26+
4. **`getValue(String formula)`**: This method parses the formula string to extract the components (X and Y). It checks if they are cell references or integers, retrieves their values (defaulting to 0 for unset cells), and computes the sum. The parsing can be done using string manipulation techniques.
27+
28+
### Complexity:
29+
- **Time Complexity**:
30+
- `setCell`: O(1) for direct updates.
31+
- `resetCell`: O(1) for direct resets.
32+
- `getValue`: O(1) for retrieving values, but parsing the formula may take O(n) where n is the length of the formula string. In the worst case, this could be O(1) since the formula is always in the format "=X+Y" with a fixed length.
33+
34+
- **Space Complexity**: O(rows * 26) for the 2D array and O(1) for the dictionary, as it only stores values for cells that have been set.
35+
36+
### Conclusion:
37+
This approach efficiently manages the spreadsheet's state and allows for quick updates and evaluations of cell values, adhering to the constraints provided. The use of a 2D array and a dictionary ensures that operations are performed in constant time for most cases, making the solution both effective and scalable.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class Spreadsheet {
5+
private int rows;
6+
private int[][] cells;
7+
private Map<String, Integer> cellMap;
8+
9+
public Spreadsheet(int rows) {
10+
this.rows = rows;
11+
this.cells = new int[rows][26];
12+
this.cellMap = new HashMap<>();
13+
}
14+
15+
public void setCell(String cell, int value) {
16+
int row = Integer.parseInt(cell.substring(1)) - 1;
17+
int col = cell.charAt(0) - 'A';
18+
cells[row][col] = value;
19+
cellMap.put(cell, value);
20+
}
21+
22+
public void resetCell(String cell) {
23+
int row = Integer.parseInt(cell.substring(1)) - 1;
24+
int col = cell.charAt(0) - 'A';
25+
cells[row][col] = 0;
26+
cellMap.put(cell, 0);
27+
}
28+
29+
public int getValue(String formula) {
30+
if (formula.charAt(0) == '=') {
31+
String[] parts = formula.substring(1).split("\\+");
32+
int sum = 0;
33+
for (String part : parts) {
34+
if (Character.isDigit(part.charAt(0))) {
35+
sum += Integer.parseInt(part);
36+
} else {
37+
sum += cellMap.getOrDefault(part, 0);
38+
}
39+
}
40+
return sum;
41+
}
42+
return 0;
43+
}
44+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Spreadsheet {
2+
constructor(rows) {
3+
this.rows = rows;
4+
this.cells = new Map();
5+
}
6+
7+
setCell(cell, value) {
8+
this.cells.set(cell, value);
9+
}
10+
11+
resetCell(cell) {
12+
this.cells.delete(cell);
13+
}
14+
15+
getValue(formula) {
16+
if (formula[0] === '=') {
17+
const parts = formula.slice(1).split('+');
18+
let total = 0;
19+
for (const part of parts) {
20+
const trimmedPart = part.trim();
21+
const num = parseInt(trimmedPart);
22+
if (!isNaN(num)) {
23+
total += num;
24+
} else {
25+
total += this.cells.get(trimmedPart) || 0;
26+
}
27+
}
28+
return total;
29+
}
30+
return 0;
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Spreadsheet:
2+
def __init__(self, rows: int):
3+
self.rows = rows
4+
self.cells = {}
5+
6+
def setCell(self, cell: str, value: int) -> None:
7+
self.cells[cell] = value
8+
9+
def resetCell(self, cell: str) -> None:
10+
if cell in self.cells:
11+
del self.cells[cell]
12+
13+
def getValue(self, formula: str) -> int:
14+
if formula[0] == '=':
15+
formula = formula[1:]
16+
parts = formula.split('+')
17+
total = 0
18+
for part in parts:
19+
part = part.strip()
20+
if part.isdigit():
21+
total += int(part)
22+
else:
23+
total += self.cells.get(part, 0)
24+
return total

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,4 @@ Each problem includes:
151151
- 2025-09-15 — [Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/) (Easy) → `Easy/2025-09-15-1935-Maximum-Number-of-Words-You-Can-Type`
152152
- 2025-09-17 — [Design a Food Rating System](https://leetcode.com/problems/design-a-food-rating-system/) (Medium) → `Medium/2025-09-17-2353-Design-a-Food-Rating-System`
153153
- 2025-09-18 — [Design Task Manager](https://leetcode.com/problems/design-task-manager/) (Medium) → `Medium/2025-09-18-3408-Design-Task-Manager`
154+
- 2025-09-19 — [Design Spreadsheet](https://leetcode.com/problems/design-spreadsheet/) (Medium) → `Medium/2025-09-19-3484-Design-Spreadsheet`

0 commit comments

Comments
 (0)