LeetCode Entry
3484. Design Spreadsheet
Design Spreadsheet: setCell, resetCell, getValue(a+b)
3484. Design Spreadsheet medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1117
Problem TLDR
Design Spreadsheet: setCell, resetCell, getValue(a+b) #medium #ds
Intuition
Rows count is small 1000, we can store all in two-dimensional array. Formula are just a single shot, without going recursive.
Approach
- using a HashMap saves LOC and string parsing
- single array:
key = (c[0]-'A') * rows + c[1..].toInt()
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
// 205ms
class Spreadsheet(rows: Int) : HashMap<String, Int>() {
fun setCell(c: String, v: Int) = put(c, v)
fun resetCell(c: String) = put(c, 0)
fun getValue(f: String) = f.drop(1).split("+")
.sumOf { if (it[0].isDigit()) it.toInt() else get(it) ?: 0 }
}
// 30ms
struct Spreadsheet([i32;26001]); impl Spreadsheet {
fn new(_: i32) -> Self { Spreadsheet([0;26001]) }
fn k(&self, c: &str) -> usize { let b = c.as_bytes();
(b[0]-b’A’)as usize*1000+c[1..].parse::
```