LeetCode Entry

3484. Design Spreadsheet

19.09.2025 medium 2025 kotlin rust

Design Spreadsheet: setCell, resetCell, getValue(a+b)

3484. Design Spreadsheet medium blog post substack youtube

1.webp

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::().unwrap()} fn set_cell(&mut self, c: String, v: i32) { self.0[self.k(&c.as_str())] = v } fn reset_cell(&mut self, c: String) { self.set_cell(c, 0) } fn get_value(&self, f: String) -> i32 { f[1..].split('+').map(|t|t.parse().unwrap_or_else(|_| self.0[self.k(&t)])).sum() } }

```