diff options
author | Jack Sangdahl <[email protected]> | 2025-09-18 20:58:15 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-18 20:58:15 -0600 |
commit | c9b062a27109bc79e6dd015644509c745e696808 (patch) | |
tree | 9a888b2638cde4eb13d8d0f362a5b94fca6f9af9 | |
parent | 0d81814bf3d0d8358d1e527de88683732ed55d23 (diff) | |
download | leetcode-c9b062a27109bc79e6dd015644509c745e696808.tar.gz leetcode-c9b062a27109bc79e6dd015644509c745e696808.zip |
3484. design spreadsheet
-rw-r--r-- | design_spreadsheet.c | 98 | ||||
-rw-r--r-- | design_spreadsheet.py | 52 |
2 files changed, 150 insertions, 0 deletions
diff --git a/design_spreadsheet.c b/design_spreadsheet.c new file mode 100644 index 0000000..6434592 --- /dev/null +++ b/design_spreadsheet.c @@ -0,0 +1,98 @@ +// 3484. Design Spreadsheet +#include "leetcode.h" + +/* + * 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. note: if 'spreadsheetGetValue' references a cell that has + * not been explicitly set using 'spreadsheetSetCell', its value is considered + * to be 0. + */ + +typedef struct { + int rows; + int size; + int capacity; + int *values; + char **keys; +} Spreadsheet; + +Spreadsheet *spreadsheetCreate(int rows) { + Spreadsheet *obj = (Spreadsheet *)malloc(sizeof(Spreadsheet)); + obj->rows = rows; + obj->size = 0; + obj->capacity = 100; + obj->values = (int *)malloc(obj->capacity * sizeof(int)); + obj->keys = (char **)malloc(obj->capacity * sizeof(char *)); + return obj; +} + +void spreadsheetSetCell(Spreadsheet *obj, char *cell, int value) { + for (int i = 0; i < obj->size; ++i) + if (!strcmp(obj->keys[i], cell)) { + obj->values[i] = value; + return; + } + if (obj->size == obj->capacity) { + obj->capacity *= 2; + obj->values = (int *)realloc(obj->values, obj->capacity * sizeof(int)); + obj->keys = (char **)realloc(obj->keys, obj->capacity * sizeof(char *)); + } + obj->keys[obj->size] = strdup(cell); + obj->values[obj->size] = value; + obj->size++; +} + +void spreadsheetResetCell(Spreadsheet *obj, char *cell) { + for (int i = 0; i < obj->size; ++i) + if (!strcmp(obj->keys[i], cell)) { + free(obj->keys[i]); + for (int j = i; j < obj->size - 1; ++j) { + obj->keys[j] = obj->keys[j + 1]; + obj->values[j] = obj->values[j + 1]; + } + obj->size--; + return; + } +} + +int get_val(Spreadsheet *obj, char *str) { + for (int i = 0; str[i]; i++) + if (!isdigit(str[i])) { + for (int j = 0; j < obj->size; ++j) + if (!strcmp(obj->keys[j], str)) + return obj->values[j]; + return 0; + } + return atoi(str); +} + +int spreadsheetGetValue(Spreadsheet *obj, char *formula) { + formula++; + char *plus = strchr(formula, '+'); + *plus = '\0'; + char *a = formula, *b = plus + 1; + return get_val(obj, a) + get_val(obj, b); +} + +void spreadsheetFree(Spreadsheet *obj) { + for (int i = 0; i < obj->size; ++i) + free(obj->keys[i]); + free(obj->keys); + free(obj->values); + free(obj); +} + +int main() { + Spreadsheet *obj = spreadsheetCreate(3); + char *f1 = "=5+7", *f2 = "=A1+6", *f3 = "=A1+B2", *f4 = "=A1+B2"; + char *c1 = "A1", *c2 = "B2"; + printf("%d\n", spreadsheetGetValue(obj, f1)); // expect: 12 + spreadsheetSetCell(obj, c1, 10); + printf("%d\n", spreadsheetGetValue(obj, f2)); // expect: 16 + spreadsheetSetCell(obj, c2, 15); + printf("%d\n", spreadsheetGetValue(obj, f3)); // expect: 25 + spreadsheetResetCell(obj, c1); + printf("%d\n", spreadsheetGetValue(obj, f4)); // expect: 15 + spreadsheetFree(obj); +} diff --git a/design_spreadsheet.py b/design_spreadsheet.py new file mode 100644 index 0000000..bd26a29 --- /dev/null +++ b/design_spreadsheet.py @@ -0,0 +1,52 @@ +# 3484. Design Spreadsheet + +""" +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. note: if 'spreadsheetGetValue' references a cell that has +not been explicitly set using 'spreadsheetSetCell', its value is considered +to be 0. +""" + + +class Spreadsheet(object): + def __init__(self, rows): + """ + :type rows: int + """ + self.rows = rows + self.cells = {} + + def setCell(self, cell, value): + """ + :type cell: str + :type value: int + :rtype: None + """ + self.cells[cell] = value + + def resetCell(self, cell): + """ + :type cell: str + :rtype: None + """ + self.cells.pop(cell, None) + + def getValue(self, formula): + """ + :type formula: str + :rtype: int + """ + parts = formula[1:].split("+") + return sum(int(p) if p.isdigit() else self.cells.get(p, 0) for p in parts) + + +if __name__ == "__main__": + obj = Spreadsheet(3) + print(obj.getValue(formula="=5+7")) + obj.setCell(cell="A1", value=10) + print(obj.getValue(formula="=A1+6")) + obj.setCell(cell="B2", value=15) + print(obj.getValue(formula="=A1+B2")) + obj.resetCell(cell="A1") + print(obj.getValue(formula="=A1+B2")) |