From 7e07ac9d83fbbdd4b8bc0611e176920c4c775976 Mon Sep 17 00:00:00 2001 From: chayan das Date: Fri, 19 Sep 2025 22:36:59 +0530 Subject: [PATCH] Create 3484. Design Spreadsheet --- 3484. Design Spreadsheet | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 3484. Design Spreadsheet diff --git a/3484. Design Spreadsheet b/3484. Design Spreadsheet new file mode 100644 index 0000000..a1051e9 --- /dev/null +++ b/3484. Design Spreadsheet @@ -0,0 +1,43 @@ +class Spreadsheet { +public: + using int2=pair; + int A[1000][26]; + int rows; + Spreadsheet(int rows) : rows(rows) { + memset(A, 0, rows*26*sizeof(int)); + } + + int2 idx(const string& cell){ + char col=cell[0]-'A'; + short row=stoi(cell.substr(1))-1; + return {row, col}; + } + void setCell(const string& cell, int value) { + A[idx(cell).first][idx(cell).second]=value; + } + + void resetCell(const string& cell) { + A[idx(cell).first][idx(cell).second]=0; + } + + int getValue(const string& formula) { + int m=formula.find('+'); + int x, y; + const string xx=formula.substr(1, m-1), yy=formula.substr(m+1); + if (formula[1]>='A') + x=A[idx(xx).first][idx(xx).second]; + else x=stoi(xx); + if (formula[m+1]>='A') + y=A[idx(yy).first][idx(yy).second]; + else y=stoi(yy); + return x+y; + } +}; + +/** + * Your Spreadsheet object will be instantiated and called as such: + * Spreadsheet* obj = new Spreadsheet(rows); + * obj->setCell(cell,value); + * obj->resetCell(cell); + * int param_3 = obj->getValue(formula); + */