diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md index ad1b60e4c9f2b..5310cc322db60 100644 --- a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md @@ -75,32 +75,141 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3483.Un -### 方法一 +### 方法一:哈希表 + 枚举 + +我们用一个哈希表 $\textit{s}$ 记录所有不同的三位偶数,然后枚举所有可能的三位偶数,将其加入哈希表中。 + +最后返回哈希表的大小即可。 + +时间复杂度 $O(n^3)$,空间复杂度 $O(n^3)$。其中 $n$ 为数组 $\textit{digits}$ 的长度。 #### Python3 ```python - +class Solution: + def totalNumbers(self, digits: List[int]) -> int: + s = set() + for i, a in enumerate(digits): + if a & 1: + continue + for j, b in enumerate(digits): + if i == j: + continue + for k, c in enumerate(digits): + if c == 0 or k in (i, j): + continue + s.add(c * 100 + b * 10 + a) + return len(s) ``` #### Java ```java - +class Solution { + public int totalNumbers(int[] digits) { + Set s = new HashSet<>(); + int n = digits.length; + for (int i = 0; i < n; ++i) { + if (digits[i] % 2 == 1) { + continue; + } + for (int j = 0; j < n; ++j) { + if (i == j) { + continue; + } + for (int k = 0; k < n; ++k) { + if (digits[k] == 0 || k == i || k == j) { + continue; + } + s.add(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int totalNumbers(vector& digits) { + unordered_set s; + int n = digits.size(); + for (int i = 0; i < n; ++i) { + if (digits[i] % 2 == 1) { + continue; + } + for (int j = 0; j < n; ++j) { + if (i == j) { + continue; + } + for (int k = 0; k < n; ++k) { + if (digits[k] == 0 || k == i || k == j) { + continue; + } + s.insert(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size(); + } +}; ``` #### Go ```go +func totalNumbers(digits []int) int { + s := make(map[int]struct{}) + for i, a := range digits { + if a%2 == 1 { + continue + } + for j, b := range digits { + if i == j { + continue + } + for k, c := range digits { + if c == 0 || k == i || k == j { + continue + } + s[c*100+b*10+a] = struct{}{} + } + } + } + return len(s) +} +``` +#### TypeScript + +```ts +function totalNumbers(digits: number[]): number { + const s = new Set(); + const n = digits.length; + for (let i = 0; i < n; ++i) { + if (digits[i] % 2 === 1) { + continue; + } + for (let j = 0; j < n; ++j) { + if (i === j) { + continue; + } + for (let k = 0; k < n; ++k) { + if (digits[k] === 0 || k === i || k === j) { + continue; + } + s.add(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size; +} ``` diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md index ced0c528062ca..4db4008b0ed56 100644 --- a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/README_EN.md @@ -73,32 +73,141 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3483.Un -### Solution 1 +### Solution 1: Hash Set + Enumeration + +We use a hash set $\textit{s}$ to record all distinct three-digit even numbers, and then enumerate all possible three-digit even numbers to add them to the hash set. + +Finally, we return the size of the hash set. + +The time complexity is $O(n^3)$, and the space complexity is $O(n^3)$. Where $n$ is the length of the array $\textit{digits}$. #### Python3 ```python - +class Solution: + def totalNumbers(self, digits: List[int]) -> int: + s = set() + for i, a in enumerate(digits): + if a & 1: + continue + for j, b in enumerate(digits): + if i == j: + continue + for k, c in enumerate(digits): + if c == 0 or k in (i, j): + continue + s.add(c * 100 + b * 10 + a) + return len(s) ``` #### Java ```java - +class Solution { + public int totalNumbers(int[] digits) { + Set s = new HashSet<>(); + int n = digits.length; + for (int i = 0; i < n; ++i) { + if (digits[i] % 2 == 1) { + continue; + } + for (int j = 0; j < n; ++j) { + if (i == j) { + continue; + } + for (int k = 0; k < n; ++k) { + if (digits[k] == 0 || k == i || k == j) { + continue; + } + s.add(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int totalNumbers(vector& digits) { + unordered_set s; + int n = digits.size(); + for (int i = 0; i < n; ++i) { + if (digits[i] % 2 == 1) { + continue; + } + for (int j = 0; j < n; ++j) { + if (i == j) { + continue; + } + for (int k = 0; k < n; ++k) { + if (digits[k] == 0 || k == i || k == j) { + continue; + } + s.insert(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size(); + } +}; ``` #### Go ```go +func totalNumbers(digits []int) int { + s := make(map[int]struct{}) + for i, a := range digits { + if a%2 == 1 { + continue + } + for j, b := range digits { + if i == j { + continue + } + for k, c := range digits { + if c == 0 || k == i || k == j { + continue + } + s[c*100+b*10+a] = struct{}{} + } + } + } + return len(s) +} +``` +#### TypeScript + +```ts +function totalNumbers(digits: number[]): number { + const s = new Set(); + const n = digits.length; + for (let i = 0; i < n; ++i) { + if (digits[i] % 2 === 1) { + continue; + } + for (let j = 0; j < n; ++j) { + if (i === j) { + continue; + } + for (let k = 0; k < n; ++k) { + if (digits[k] === 0 || k === i || k === j) { + continue; + } + s.add(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size; +} ``` diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.cpp b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.cpp new file mode 100644 index 0000000000000..6b3291b643e04 --- /dev/null +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int totalNumbers(vector& digits) { + unordered_set s; + int n = digits.size(); + for (int i = 0; i < n; ++i) { + if (digits[i] % 2 == 1) { + continue; + } + for (int j = 0; j < n; ++j) { + if (i == j) { + continue; + } + for (int k = 0; k < n; ++k) { + if (digits[k] == 0 || k == i || k == j) { + continue; + } + s.insert(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size(); + } +}; diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.go b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.go new file mode 100644 index 0000000000000..23687fd9345e5 --- /dev/null +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.go @@ -0,0 +1,20 @@ +func totalNumbers(digits []int) int { + s := make(map[int]struct{}) + for i, a := range digits { + if a%2 == 1 { + continue + } + for j, b := range digits { + if i == j { + continue + } + for k, c := range digits { + if c == 0 || k == i || k == j { + continue + } + s[c*100+b*10+a] = struct{}{} + } + } + } + return len(s) +} diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.java b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.java new file mode 100644 index 0000000000000..0f14750fb0f6b --- /dev/null +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int totalNumbers(int[] digits) { + Set s = new HashSet<>(); + int n = digits.length; + for (int i = 0; i < n; ++i) { + if (digits[i] % 2 == 1) { + continue; + } + for (int j = 0; j < n; ++j) { + if (i == j) { + continue; + } + for (int k = 0; k < n; ++k) { + if (digits[k] == 0 || k == i || k == j) { + continue; + } + s.add(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size(); + } +} diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.py b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.py new file mode 100644 index 0000000000000..166b7853fe10d --- /dev/null +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def totalNumbers(self, digits: List[int]) -> int: + s = set() + for i, a in enumerate(digits): + if a & 1: + continue + for j, b in enumerate(digits): + if i == j: + continue + for k, c in enumerate(digits): + if c == 0 or k in (i, j): + continue + s.add(c * 100 + b * 10 + a) + return len(s) diff --git a/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.ts b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.ts new file mode 100644 index 0000000000000..32790093dd693 --- /dev/null +++ b/solution/3400-3499/3483.Unique 3-Digit Even Numbers/Solution.ts @@ -0,0 +1,21 @@ +function totalNumbers(digits: number[]): number { + const s = new Set(); + const n = digits.length; + for (let i = 0; i < n; ++i) { + if (digits[i] % 2 === 1) { + continue; + } + for (let j = 0; j < n; ++j) { + if (i === j) { + continue; + } + for (let k = 0; k < n; ++k) { + if (digits[k] === 0 || k === i || k === j) { + continue; + } + s.add(digits[k] * 100 + digits[j] * 10 + digits[i]); + } + } + } + return s.size; +} diff --git a/solution/3400-3499/3484.Design Spreadsheet/README.md b/solution/3400-3499/3484.Design Spreadsheet/README.md index 90ac042d3f9b8..b07c001613d8b 100644 --- a/solution/3400-3499/3484.Design Spreadsheet/README.md +++ b/solution/3400-3499/3484.Design Spreadsheet/README.md @@ -67,32 +67,203 @@ spreadsheet.getValue("=A1+B2"); // 返回 15 (0+15) -### 方法一 +### 方法一:哈希表 + +我们用一个哈希表 $\textit{d}$ 来记录所有单元格的值,其中键为单元格引用,值为单元格的值。 + +调用 `setCell` 方法时,我们将单元格引用和值存入哈希表中。 + +调用 `resetCell` 方法时,我们将单元格引用从哈希表中删除。 + +调用 `getValue` 方法时,我们将公式分割成两个单元格引用,然后计算它们的值,最后返回它们的和。 + +时间复杂度 $(L)$,空间复杂度 $(L)$。其中 $L$ 为公式的长度。 #### Python3 ```python +class Spreadsheet: + + def __init__(self, rows: int): + self.d = {} + + def setCell(self, cell: str, value: int) -> None: + self.d[cell] = value + def resetCell(self, cell: str) -> None: + self.d.pop(cell, None) + + def getValue(self, formula: str) -> int: + ans = 0 + for cell in formula[1:].split("+"): + ans += int(cell) if cell[0].isdigit() else self.d.get(cell, 0) + return ans + + +# Your Spreadsheet object will be instantiated and called as such: +# obj = Spreadsheet(rows) +# obj.setCell(cell,value) +# obj.resetCell(cell) +# param_3 = obj.getValue(formula) ``` #### Java ```java - +class Spreadsheet { + private Map d = new HashMap<>(); + + public Spreadsheet(int rows) { + } + + public void setCell(String cell, int value) { + d.put(cell, value); + } + + public void resetCell(String cell) { + d.remove(cell); + } + + public int getValue(String formula) { + int ans = 0; + for (String cell : formula.substring(1).split("\\+")) { + ans += Character.isDigit(cell.charAt(0)) ? Integer.parseInt(cell) + : d.getOrDefault(cell, 0); + } + return ans; + } +} + +/** + * 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); + */ ``` #### C++ ```cpp - +class Spreadsheet { +private: + unordered_map d; + +public: + Spreadsheet(int rows) {} + + void setCell(string cell, int value) { + d[cell] = value; + } + + void resetCell(string cell) { + d.erase(cell); + } + + int getValue(string formula) { + int ans = 0; + stringstream ss(formula.substr(1)); + string cell; + while (getline(ss, cell, '+')) { + if (isdigit(cell[0])) { + ans += stoi(cell); + } else { + ans += d.count(cell) ? d[cell] : 0; + } + } + return ans; + } +}; + +/** + * 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); + */ ``` #### Go ```go +type Spreadsheet struct { + d map[string]int +} + +func Constructor(rows int) Spreadsheet { + return Spreadsheet{d: make(map[string]int)} +} + +func (this *Spreadsheet) SetCell(cell string, value int) { + this.d[cell] = value +} + +func (this *Spreadsheet) ResetCell(cell string) { + delete(this.d, cell) +} + +func (this *Spreadsheet) GetValue(formula string) int { + ans := 0 + cells := strings.Split(formula[1:], "+") + for _, cell := range cells { + if val, err := strconv.Atoi(cell); err == nil { + ans += val + } else { + ans += this.d[cell] + } + } + return ans +} + + +/** + * Your Spreadsheet object will be instantiated and called as such: + * obj := Constructor(rows); + * obj.SetCell(cell,value); + * obj.ResetCell(cell); + * param_3 := obj.GetValue(formula); + */ +``` +#### TypeScript + +```ts +class Spreadsheet { + private d: Map; + + constructor(rows: number) { + this.d = new Map(); + } + + setCell(cell: string, value: number): void { + this.d.set(cell, value); + } + + resetCell(cell: string): void { + this.d.delete(cell); + } + + getValue(formula: string): number { + let ans = 0; + const cells = formula.slice(1).split('+'); + for (const cell of cells) { + ans += isNaN(Number(cell)) ? this.d.get(cell) || 0 : Number(cell); + } + return ans; + } +} + +/** + * Your Spreadsheet object will be instantiated and called as such: + * var obj = new Spreadsheet(rows) + * obj.setCell(cell,value) + * obj.resetCell(cell) + * var param_3 = obj.getValue(formula) + */ ``` diff --git a/solution/3400-3499/3484.Design Spreadsheet/README_EN.md b/solution/3400-3499/3484.Design Spreadsheet/README_EN.md index a3c174eb04fc6..59fb165db6f2c 100644 --- a/solution/3400-3499/3484.Design Spreadsheet/README_EN.md +++ b/solution/3400-3499/3484.Design Spreadsheet/README_EN.md @@ -65,32 +65,203 @@ spreadsheet.getValue("=A1+B2"); // returns 15 (0+15) -### Solution 1 +### Solution 1: Hash Table + +We use a hash table $\textit{d}$ to record the values of all cells, where the key is the cell reference and the value is the cell's value. + +When calling the `setCell` method, we store the cell reference and value in the hash table. + +When calling the `resetCell` method, we remove the cell reference from the hash table. + +When calling the `getValue` method, we split the formula into two cell references, calculate their values, and then return their sum. + +The time complexity is $O(L)$, and the space complexity is $O(L)$. Where $L$ is the length of the formula. #### Python3 ```python +class Spreadsheet: + + def __init__(self, rows: int): + self.d = {} + + def setCell(self, cell: str, value: int) -> None: + self.d[cell] = value + def resetCell(self, cell: str) -> None: + self.d.pop(cell, None) + + def getValue(self, formula: str) -> int: + ans = 0 + for cell in formula[1:].split("+"): + ans += int(cell) if cell[0].isdigit() else self.d.get(cell, 0) + return ans + + +# Your Spreadsheet object will be instantiated and called as such: +# obj = Spreadsheet(rows) +# obj.setCell(cell,value) +# obj.resetCell(cell) +# param_3 = obj.getValue(formula) ``` #### Java ```java - +class Spreadsheet { + private Map d = new HashMap<>(); + + public Spreadsheet(int rows) { + } + + public void setCell(String cell, int value) { + d.put(cell, value); + } + + public void resetCell(String cell) { + d.remove(cell); + } + + public int getValue(String formula) { + int ans = 0; + for (String cell : formula.substring(1).split("\\+")) { + ans += Character.isDigit(cell.charAt(0)) ? Integer.parseInt(cell) + : d.getOrDefault(cell, 0); + } + return ans; + } +} + +/** + * 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); + */ ``` #### C++ ```cpp - +class Spreadsheet { +private: + unordered_map d; + +public: + Spreadsheet(int rows) {} + + void setCell(string cell, int value) { + d[cell] = value; + } + + void resetCell(string cell) { + d.erase(cell); + } + + int getValue(string formula) { + int ans = 0; + stringstream ss(formula.substr(1)); + string cell; + while (getline(ss, cell, '+')) { + if (isdigit(cell[0])) { + ans += stoi(cell); + } else { + ans += d.count(cell) ? d[cell] : 0; + } + } + return ans; + } +}; + +/** + * 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); + */ ``` #### Go ```go +type Spreadsheet struct { + d map[string]int +} + +func Constructor(rows int) Spreadsheet { + return Spreadsheet{d: make(map[string]int)} +} + +func (this *Spreadsheet) SetCell(cell string, value int) { + this.d[cell] = value +} + +func (this *Spreadsheet) ResetCell(cell string) { + delete(this.d, cell) +} + +func (this *Spreadsheet) GetValue(formula string) int { + ans := 0 + cells := strings.Split(formula[1:], "+") + for _, cell := range cells { + if val, err := strconv.Atoi(cell); err == nil { + ans += val + } else { + ans += this.d[cell] + } + } + return ans +} + + +/** + * Your Spreadsheet object will be instantiated and called as such: + * obj := Constructor(rows); + * obj.SetCell(cell,value); + * obj.ResetCell(cell); + * param_3 := obj.GetValue(formula); + */ +``` +#### TypeScript + +```ts +class Spreadsheet { + private d: Map; + + constructor(rows: number) { + this.d = new Map(); + } + + setCell(cell: string, value: number): void { + this.d.set(cell, value); + } + + resetCell(cell: string): void { + this.d.delete(cell); + } + + getValue(formula: string): number { + let ans = 0; + const cells = formula.slice(1).split('+'); + for (const cell of cells) { + ans += isNaN(Number(cell)) ? this.d.get(cell) || 0 : Number(cell); + } + return ans; + } +} + +/** + * Your Spreadsheet object will be instantiated and called as such: + * var obj = new Spreadsheet(rows) + * obj.setCell(cell,value) + * obj.resetCell(cell) + * var param_3 = obj.getValue(formula) + */ ``` diff --git a/solution/3400-3499/3484.Design Spreadsheet/Solution.cpp b/solution/3400-3499/3484.Design Spreadsheet/Solution.cpp new file mode 100644 index 0000000000000..d0eaaceb5fcee --- /dev/null +++ b/solution/3400-3499/3484.Design Spreadsheet/Solution.cpp @@ -0,0 +1,37 @@ +class Spreadsheet { +private: + unordered_map d; + +public: + Spreadsheet(int rows) {} + + void setCell(string cell, int value) { + d[cell] = value; + } + + void resetCell(string cell) { + d.erase(cell); + } + + int getValue(string formula) { + int ans = 0; + stringstream ss(formula.substr(1)); + string cell; + while (getline(ss, cell, '+')) { + if (isdigit(cell[0])) { + ans += stoi(cell); + } else { + ans += d.count(cell) ? d[cell] : 0; + } + } + return ans; + } +}; + +/** + * 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); + */ diff --git a/solution/3400-3499/3484.Design Spreadsheet/Solution.go b/solution/3400-3499/3484.Design Spreadsheet/Solution.go new file mode 100644 index 0000000000000..56e8b6c953b3f --- /dev/null +++ b/solution/3400-3499/3484.Design Spreadsheet/Solution.go @@ -0,0 +1,37 @@ +type Spreadsheet struct { + d map[string]int +} + +func Constructor(rows int) Spreadsheet { + return Spreadsheet{d: make(map[string]int)} +} + +func (this *Spreadsheet) SetCell(cell string, value int) { + this.d[cell] = value +} + +func (this *Spreadsheet) ResetCell(cell string) { + delete(this.d, cell) +} + +func (this *Spreadsheet) GetValue(formula string) int { + ans := 0 + cells := strings.Split(formula[1:], "+") + for _, cell := range cells { + if val, err := strconv.Atoi(cell); err == nil { + ans += val + } else { + ans += this.d[cell] + } + } + return ans +} + + +/** + * Your Spreadsheet object will be instantiated and called as such: + * obj := Constructor(rows); + * obj.SetCell(cell,value); + * obj.ResetCell(cell); + * param_3 := obj.GetValue(formula); + */ diff --git a/solution/3400-3499/3484.Design Spreadsheet/Solution.java b/solution/3400-3499/3484.Design Spreadsheet/Solution.java new file mode 100644 index 0000000000000..ec80f2484c0e7 --- /dev/null +++ b/solution/3400-3499/3484.Design Spreadsheet/Solution.java @@ -0,0 +1,31 @@ +class Spreadsheet { + private Map d = new HashMap<>(); + + public Spreadsheet(int rows) { + } + + public void setCell(String cell, int value) { + d.put(cell, value); + } + + public void resetCell(String cell) { + d.remove(cell); + } + + public int getValue(String formula) { + int ans = 0; + for (String cell : formula.substring(1).split("\\+")) { + ans += Character.isDigit(cell.charAt(0)) ? Integer.parseInt(cell) + : d.getOrDefault(cell, 0); + } + return ans; + } +} + +/** + * 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); + */ diff --git a/solution/3400-3499/3484.Design Spreadsheet/Solution.py b/solution/3400-3499/3484.Design Spreadsheet/Solution.py new file mode 100644 index 0000000000000..e1149e946321b --- /dev/null +++ b/solution/3400-3499/3484.Design Spreadsheet/Solution.py @@ -0,0 +1,22 @@ +class Spreadsheet: + def __init__(self, rows: int): + self.d = {} + + def setCell(self, cell: str, value: int) -> None: + self.d[cell] = value + + def resetCell(self, cell: str) -> None: + self.d.pop(cell, None) + + def getValue(self, formula: str) -> int: + ans = 0 + for cell in formula[1:].split("+"): + ans += int(cell) if cell[0].isdigit() else self.d.get(cell, 0) + return ans + + +# Your Spreadsheet object will be instantiated and called as such: +# obj = Spreadsheet(rows) +# obj.setCell(cell,value) +# obj.resetCell(cell) +# param_3 = obj.getValue(formula) diff --git a/solution/3400-3499/3484.Design Spreadsheet/Solution.ts b/solution/3400-3499/3484.Design Spreadsheet/Solution.ts new file mode 100644 index 0000000000000..eb9165d0c8cb3 --- /dev/null +++ b/solution/3400-3499/3484.Design Spreadsheet/Solution.ts @@ -0,0 +1,32 @@ +class Spreadsheet { + private d: Map; + + constructor(rows: number) { + this.d = new Map(); + } + + setCell(cell: string, value: number): void { + this.d.set(cell, value); + } + + resetCell(cell: string): void { + this.d.delete(cell); + } + + getValue(formula: string): number { + let ans = 0; + const cells = formula.slice(1).split('+'); + for (const cell of cells) { + ans += isNaN(Number(cell)) ? this.d.get(cell) || 0 : Number(cell); + } + return ans; + } +} + +/** + * Your Spreadsheet object will be instantiated and called as such: + * var obj = new Spreadsheet(rows) + * obj.setCell(cell,value) + * obj.resetCell(cell) + * var param_3 = obj.getValue(formula) + */ diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md index f24309194b11e..44c6f26993998 100644 --- a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README.md @@ -79,32 +79,120 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3487.Ma -### 方法一 +### 方法一:贪心 + 哈希表 + +我们先找出数组中的最大值 $\textit{mx}$,如果 $\textit{mx} \leq 0$,那么数组中所有元素都小于等于 $0$,由于需要选出一个元素和最大的非空子数组,那么最大元素和就是 $\textit{mx}$。 + +如果 $\textit{mx} > 0$,那么我们需要找出数组中所有不同的正整数,并且这些正整数的和最大。我们可以使用一个哈希表 $\textit{s}$ 来记录所有不同的正整数,然后遍历数组,将所有不同的正整数加起来即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def maxSum(self, nums: List[int]) -> int: + mx = max(nums) + if mx <= 0: + return mx + ans = 0 + s = set() + for x in nums: + if x < 0 or x in s: + continue + ans += x + s.add(x) + return ans ``` #### Java ```java - +class Solution { + public int maxSum(int[] nums) { + int mx = Arrays.stream(nums).max().getAsInt(); + if (mx <= 0) { + return mx; + } + boolean[] s = new boolean[201]; + int ans = 0; + for (int x : nums) { + if (x < 0 || s[x]) { + continue; + } + ans += x; + s[x] = true; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxSum(vector& nums) { + int mx = ranges::max(nums); + if (mx <= 0) { + return mx; + } + unordered_set s; + int ans = 0; + for (int x : nums) { + if (x < 0 || s.contains(x)) { + continue; + } + ans += x; + s.insert(x); + } + return ans; + } +}; ``` #### Go ```go +func maxSum(nums []int) (ans int) { + mx := slices.Max(nums) + if mx <= 0 { + return mx + } + s := make(map[int]bool) + for _, x := range nums { + if x < 0 || s[x] { + continue + } + ans += x + s[x] = true + } + return +} +``` +#### TypeScript + +```ts +function maxSum(nums: number[]): number { + const mx = Math.max(...nums); + if (mx <= 0) { + return mx; + } + const s = new Set(); + let ans: number = 0; + for (const x of nums) { + if (x < 0 || s.has(x)) { + continue; + } + ans += x; + s.add(x); + } + return ans; +} ``` diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md index 799f63f7a6f7d..3d7bedab4ec87 100644 --- a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/README_EN.md @@ -76,32 +76,120 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3487.Ma -### Solution 1 +### Solution 1: Greedy + Hash Table + +We first find the maximum value $\textit{mx}$ in the array. If $\textit{mx} \leq 0$, then all elements in the array are less than or equal to 0. Since we need to select a non-empty subarray with the maximum element sum, the maximum element sum would be $\textit{mx}$. + +If $\textit{mx} > 0$, then we need to find all distinct positive integers in the array such that their sum is maximized. We can use a hash table $\textit{s}$ to record all distinct positive integers, and then iterate through the array, adding up all distinct positive integers. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{nums}$. #### Python3 ```python - +class Solution: + def maxSum(self, nums: List[int]) -> int: + mx = max(nums) + if mx <= 0: + return mx + ans = 0 + s = set() + for x in nums: + if x < 0 or x in s: + continue + ans += x + s.add(x) + return ans ``` #### Java ```java - +class Solution { + public int maxSum(int[] nums) { + int mx = Arrays.stream(nums).max().getAsInt(); + if (mx <= 0) { + return mx; + } + boolean[] s = new boolean[201]; + int ans = 0; + for (int x : nums) { + if (x < 0 || s[x]) { + continue; + } + ans += x; + s[x] = true; + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxSum(vector& nums) { + int mx = ranges::max(nums); + if (mx <= 0) { + return mx; + } + unordered_set s; + int ans = 0; + for (int x : nums) { + if (x < 0 || s.contains(x)) { + continue; + } + ans += x; + s.insert(x); + } + return ans; + } +}; ``` #### Go ```go +func maxSum(nums []int) (ans int) { + mx := slices.Max(nums) + if mx <= 0 { + return mx + } + s := make(map[int]bool) + for _, x := range nums { + if x < 0 || s[x] { + continue + } + ans += x + s[x] = true + } + return +} +``` +#### TypeScript + +```ts +function maxSum(nums: number[]): number { + const mx = Math.max(...nums); + if (mx <= 0) { + return mx; + } + const s = new Set(); + let ans: number = 0; + for (const x of nums) { + if (x < 0 || s.has(x)) { + continue; + } + ans += x; + s.add(x); + } + return ans; +} ``` diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.cpp b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.cpp new file mode 100644 index 0000000000000..4ff264db39929 --- /dev/null +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int maxSum(vector& nums) { + int mx = ranges::max(nums); + if (mx <= 0) { + return mx; + } + unordered_set s; + int ans = 0; + for (int x : nums) { + if (x < 0 || s.contains(x)) { + continue; + } + ans += x; + s.insert(x); + } + return ans; + } +}; diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.go b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.go new file mode 100644 index 0000000000000..82c2af0d4bb01 --- /dev/null +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.go @@ -0,0 +1,15 @@ +func maxSum(nums []int) (ans int) { + mx := slices.Max(nums) + if mx <= 0 { + return mx + } + s := make(map[int]bool) + for _, x := range nums { + if x < 0 || s[x] { + continue + } + ans += x + s[x] = true + } + return +} diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.java b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.java new file mode 100644 index 0000000000000..bb58fed5e7899 --- /dev/null +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int maxSum(int[] nums) { + int mx = Arrays.stream(nums).max().getAsInt(); + if (mx <= 0) { + return mx; + } + boolean[] s = new boolean[201]; + int ans = 0; + for (int x : nums) { + if (x < 0 || s[x]) { + continue; + } + ans += x; + s[x] = true; + } + return ans; + } +} diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.py b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.py new file mode 100644 index 0000000000000..93118e0e090de --- /dev/null +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def maxSum(self, nums: List[int]) -> int: + mx = max(nums) + if mx <= 0: + return mx + ans = 0 + s = set() + for x in nums: + if x < 0 or x in s: + continue + ans += x + s.add(x) + return ans diff --git a/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.ts b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.ts new file mode 100644 index 0000000000000..c529a3041d385 --- /dev/null +++ b/solution/3400-3499/3487.Maximum Unique Subarray Sum After Deletion/Solution.ts @@ -0,0 +1,16 @@ +function maxSum(nums: number[]): number { + const mx = Math.max(...nums); + if (mx <= 0) { + return mx; + } + const s = new Set(); + let ans: number = 0; + for (const x of nums) { + if (x < 0 || s.has(x)) { + continue; + } + ans += x; + s.add(x); + } + return ans; +}