From d2291a9c94fd5032834971f7ee8f2282ed4822b3 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 24 Apr 2024 10:20:01 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.1146,2639 * No.1146.Snapshot Array * No.2639.Find the Width of Columns of a Grid --- .../1100-1199/1146.Snapshot Array/README.md | 125 +++++++++++------- .../1146.Snapshot Array/README_EN.md | 125 +++++++++++------- .../1146.Snapshot Array/Solution.cpp | 22 +-- .../1100-1199/1146.Snapshot Array/Solution.go | 21 ++- .../1146.Snapshot Array/Solution.java | 16 +-- .../1100-1199/1146.Snapshot Array/Solution.py | 16 +-- .../1100-1199/1146.Snapshot Array/Solution.ts | 37 ++++++ .../README.md | 11 +- .../README_EN.md | 17 ++- .../Solution.py | 7 +- .../Solution2.py | 3 - .../README.md | 2 +- .../README_EN.md | 8 +- 13 files changed, 244 insertions(+), 166 deletions(-) create mode 100644 solution/1100-1199/1146.Snapshot Array/Solution.ts delete mode 100644 solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution2.py diff --git a/solution/1100-1199/1146.Snapshot Array/README.md b/solution/1100-1199/1146.Snapshot Array/README.md index c8de6f503e8b5..db1b2095e7342 100644 --- a/solution/1100-1199/1146.Snapshot Array/README.md +++ b/solution/1100-1199/1146.Snapshot Array/README.md @@ -47,33 +47,35 @@ snapshotArr.get(0,0); // 获取 snap_id = 0 的快照中 array[0] 的值,返 ### 方法一:数组 + 二分查找 -维护一个数组,数组中的每个元素是一个列表,列表中存储的是每次设置的值以及对应的快照编号。 +我们维护一个长度为 $\text{length}$ 的数组,数组中的每个元素是一个列表,用来存储每次设置的值以及对应的快照 ID。 -每次设置值时,将值和快照编号添加到对应索引的列表中。 +调用 `set` 方法时,将值和快照 ID 添加到对应索引的列表中。时间复杂度 $O(1)$。 -每次获取值时,使用二分查找,找到对应位置第一个大于快照编号 `snap_id` 的值,然后返回前一个值即可。 +调用 `snap` 方法时,我们先将快照 ID 加一,然后返回快照 ID 减一。时间复杂度 $O(1)$。 -时间复杂度上,设置值的时间复杂度为 $O(1)$,快照的时间复杂度为 $O(1)$,获取值的时间复杂度为 $O(\log n)$。 +调用 `get` 方法时,我们使用二分查找找到对应位置的第一个快照 ID 大于 `snap_id` 的值,然后返回前一个的值。如果找不到,则返回 0。时间复杂度 $O(\log n)$。 + +空间复杂度 $O(n)$。 ```python class SnapshotArray: + def __init__(self, length: int): - self.idx = 0 - self.arr = defaultdict(list) + self.arr = [[] for _ in range(length)] + self.i = 0 def set(self, index: int, val: int) -> None: - self.arr[index].append((self.idx, val)) + self.arr[index].append((self.i, val)) def snap(self) -> int: - self.idx += 1 - return self.idx - 1 + self.i += 1 + return self.i - 1 def get(self, index: int, snap_id: int) -> int: - vals = self.arr[index] - i = bisect_right(vals, (snap_id, inf)) - 1 - return 0 if i < 0 else vals[i][1] + i = bisect_left(self.arr[index], (snap_id, inf)) - 1 + return 0 if i < 0 else self.arr[index][i][1] # Your SnapshotArray object will be instantiated and called as such: @@ -102,17 +104,17 @@ class SnapshotArray { } public int get(int index, int snap_id) { - var vals = arr[index]; - int left = 0, right = vals.size(); - while (left < right) { - int mid = (left + right) >> 1; - if (vals.get(mid)[0] > snap_id) { - right = mid; + int l = 0, r = arr[index].size(); + while (l < r) { + int mid = (l + r) >> 1; + if (arr[index].get(mid)[0] > snap_id) { + r = mid; } else { - left = mid + 1; + l = mid + 1; } } - return left == 0 ? 0 : vals.get(left - 1)[1]; + --l; + return l < 0 ? 0 : arr[index].get(l)[1]; } } @@ -129,35 +131,25 @@ class SnapshotArray { class SnapshotArray { public: SnapshotArray(int length) { - idx = 0; - arr = vector>>(length); + arr.resize(length); } void set(int index, int val) { - arr[index].push_back({idx, val}); + arr[index].emplace_back(i, val); } int snap() { - return idx++; + return i++; } int get(int index, int snap_id) { - auto& vals = arr[index]; - int left = 0, right = vals.size(); - while (left < right) { - int mid = (left + right) >> 1; - if (vals[mid].first > snap_id) { - right = mid; - } else { - left = mid + 1; - } - } - return left == 0 ? 0 : vals[left - 1].second; + auto it = upper_bound(arr[index].begin(), arr[index].end(), make_pair(snap_id, INT_MAX)); + return it == arr[index].begin() ? 0 : prev(it)->second; } private: vector>> arr; - int idx; + int i = 0; }; /** @@ -171,34 +163,31 @@ private: ```go type SnapshotArray struct { - idx int - arr [][]pair + arr [][][2]int + i int } func Constructor(length int) SnapshotArray { - return SnapshotArray{0, make([][]pair, length)} + return SnapshotArray{make([][][2]int, length), 0} } func (this *SnapshotArray) Set(index int, val int) { - this.arr[index] = append(this.arr[index], pair{this.idx, val}) + this.arr[index] = append(this.arr[index], [2]int{this.i, val}) } func (this *SnapshotArray) Snap() int { - this.idx++ - return this.idx - 1 + this.i++ + return this.i - 1 } func (this *SnapshotArray) Get(index int, snap_id int) int { - vals := this.arr[index] - i := sort.Search(len(vals), func(i int) bool { return vals[i].i > snap_id }) - if i == 0 { + i := sort.Search(len(this.arr[index]), func(i int) bool { return this.arr[index][i][0] > snap_id }) - 1 + if i < 0 { return 0 } - return vals[i-1].v + return this.arr[index][i][1] } -type pair struct{ i, v int } - /** * Your SnapshotArray object will be instantiated and called as such: * obj := Constructor(length); @@ -208,6 +197,46 @@ type pair struct{ i, v int } */ ``` +```ts +class SnapshotArray { + private arr: [number, number][][]; + private i: number = 0; + constructor(length: number) { + this.arr = Array.from({ length }, () => []); + } + + set(index: number, val: number): void { + this.arr[index].push([this.i, val]); + } + + snap(): number { + return this.i++; + } + + get(index: number, snap_id: number): number { + let [l, r] = [0, this.arr[index].length]; + while (l < r) { + const mid = (l + r) >> 1; + if (this.arr[index][mid][0] > snap_id) { + r = mid; + } else { + l = mid + 1; + } + } + --l; + return l < 0 ? 0 : this.arr[index][l][1]; + } +} + +/** + * Your SnapshotArray object will be instantiated and called as such: + * var obj = new SnapshotArray(length) + * obj.set(index,val) + * var param_2 = obj.snap() + * var param_3 = obj.get(index,snap_id) + */ +``` + diff --git a/solution/1100-1199/1146.Snapshot Array/README_EN.md b/solution/1100-1199/1146.Snapshot Array/README_EN.md index 28a58fe46702f..54485b01d21db 100644 --- a/solution/1100-1199/1146.Snapshot Array/README_EN.md +++ b/solution/1100-1199/1146.Snapshot Array/README_EN.md @@ -44,33 +44,35 @@ snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5 ```python class SnapshotArray: + def __init__(self, length: int): - self.idx = 0 - self.arr = defaultdict(list) + self.arr = [[] for _ in range(length)] + self.i = 0 def set(self, index: int, val: int) -> None: - self.arr[index].append((self.idx, val)) + self.arr[index].append((self.i, val)) def snap(self) -> int: - self.idx += 1 - return self.idx - 1 + self.i += 1 + return self.i - 1 def get(self, index: int, snap_id: int) -> int: - vals = self.arr[index] - i = bisect_right(vals, (snap_id, inf)) - 1 - return 0 if i < 0 else vals[i][1] + i = bisect_left(self.arr[index], (snap_id, inf)) - 1 + return 0 if i < 0 else self.arr[index][i][1] # Your SnapshotArray object will be instantiated and called as such: @@ -99,17 +101,17 @@ class SnapshotArray { } public int get(int index, int snap_id) { - var vals = arr[index]; - int left = 0, right = vals.size(); - while (left < right) { - int mid = (left + right) >> 1; - if (vals.get(mid)[0] > snap_id) { - right = mid; + int l = 0, r = arr[index].size(); + while (l < r) { + int mid = (l + r) >> 1; + if (arr[index].get(mid)[0] > snap_id) { + r = mid; } else { - left = mid + 1; + l = mid + 1; } } - return left == 0 ? 0 : vals.get(left - 1)[1]; + --l; + return l < 0 ? 0 : arr[index].get(l)[1]; } } @@ -126,35 +128,25 @@ class SnapshotArray { class SnapshotArray { public: SnapshotArray(int length) { - idx = 0; - arr = vector>>(length); + arr.resize(length); } void set(int index, int val) { - arr[index].push_back({idx, val}); + arr[index].emplace_back(i, val); } int snap() { - return idx++; + return i++; } int get(int index, int snap_id) { - auto& vals = arr[index]; - int left = 0, right = vals.size(); - while (left < right) { - int mid = (left + right) >> 1; - if (vals[mid].first > snap_id) { - right = mid; - } else { - left = mid + 1; - } - } - return left == 0 ? 0 : vals[left - 1].second; + auto it = upper_bound(arr[index].begin(), arr[index].end(), make_pair(snap_id, INT_MAX)); + return it == arr[index].begin() ? 0 : prev(it)->second; } private: vector>> arr; - int idx; + int i = 0; }; /** @@ -168,34 +160,31 @@ private: ```go type SnapshotArray struct { - idx int - arr [][]pair + arr [][][2]int + i int } func Constructor(length int) SnapshotArray { - return SnapshotArray{0, make([][]pair, length)} + return SnapshotArray{make([][][2]int, length), 0} } func (this *SnapshotArray) Set(index int, val int) { - this.arr[index] = append(this.arr[index], pair{this.idx, val}) + this.arr[index] = append(this.arr[index], [2]int{this.i, val}) } func (this *SnapshotArray) Snap() int { - this.idx++ - return this.idx - 1 + this.i++ + return this.i - 1 } func (this *SnapshotArray) Get(index int, snap_id int) int { - vals := this.arr[index] - i := sort.Search(len(vals), func(i int) bool { return vals[i].i > snap_id }) - if i == 0 { + i := sort.Search(len(this.arr[index]), func(i int) bool { return this.arr[index][i][0] > snap_id }) - 1 + if i < 0 { return 0 } - return vals[i-1].v + return this.arr[index][i][1] } -type pair struct{ i, v int } - /** * Your SnapshotArray object will be instantiated and called as such: * obj := Constructor(length); @@ -205,6 +194,46 @@ type pair struct{ i, v int } */ ``` +```ts +class SnapshotArray { + private arr: [number, number][][]; + private i: number = 0; + constructor(length: number) { + this.arr = Array.from({ length }, () => []); + } + + set(index: number, val: number): void { + this.arr[index].push([this.i, val]); + } + + snap(): number { + return this.i++; + } + + get(index: number, snap_id: number): number { + let [l, r] = [0, this.arr[index].length]; + while (l < r) { + const mid = (l + r) >> 1; + if (this.arr[index][mid][0] > snap_id) { + r = mid; + } else { + l = mid + 1; + } + } + --l; + return l < 0 ? 0 : this.arr[index][l][1]; + } +} + +/** + * Your SnapshotArray object will be instantiated and called as such: + * var obj = new SnapshotArray(length) + * obj.set(index,val) + * var param_2 = obj.snap() + * var param_3 = obj.get(index,snap_id) + */ +``` + diff --git a/solution/1100-1199/1146.Snapshot Array/Solution.cpp b/solution/1100-1199/1146.Snapshot Array/Solution.cpp index f52ed91a41a2b..fae0c520adfd6 100644 --- a/solution/1100-1199/1146.Snapshot Array/Solution.cpp +++ b/solution/1100-1199/1146.Snapshot Array/Solution.cpp @@ -1,35 +1,25 @@ class SnapshotArray { public: SnapshotArray(int length) { - idx = 0; - arr = vector>>(length); + arr.resize(length); } void set(int index, int val) { - arr[index].push_back({idx, val}); + arr[index].emplace_back(i, val); } int snap() { - return idx++; + return i++; } int get(int index, int snap_id) { - auto& vals = arr[index]; - int left = 0, right = vals.size(); - while (left < right) { - int mid = (left + right) >> 1; - if (vals[mid].first > snap_id) { - right = mid; - } else { - left = mid + 1; - } - } - return left == 0 ? 0 : vals[left - 1].second; + auto it = upper_bound(arr[index].begin(), arr[index].end(), make_pair(snap_id, INT_MAX)); + return it == arr[index].begin() ? 0 : prev(it)->second; } private: vector>> arr; - int idx; + int i = 0; }; /** diff --git a/solution/1100-1199/1146.Snapshot Array/Solution.go b/solution/1100-1199/1146.Snapshot Array/Solution.go index afbcc3cd1238b..1ad1e873b507c 100644 --- a/solution/1100-1199/1146.Snapshot Array/Solution.go +++ b/solution/1100-1199/1146.Snapshot Array/Solution.go @@ -1,32 +1,29 @@ type SnapshotArray struct { - idx int - arr [][]pair + arr [][][2]int + i int } func Constructor(length int) SnapshotArray { - return SnapshotArray{0, make([][]pair, length)} + return SnapshotArray{make([][][2]int, length), 0} } func (this *SnapshotArray) Set(index int, val int) { - this.arr[index] = append(this.arr[index], pair{this.idx, val}) + this.arr[index] = append(this.arr[index], [2]int{this.i, val}) } func (this *SnapshotArray) Snap() int { - this.idx++ - return this.idx - 1 + this.i++ + return this.i - 1 } func (this *SnapshotArray) Get(index int, snap_id int) int { - vals := this.arr[index] - i := sort.Search(len(vals), func(i int) bool { return vals[i].i > snap_id }) - if i == 0 { + i := sort.Search(len(this.arr[index]), func(i int) bool { return this.arr[index][i][0] > snap_id }) - 1 + if i < 0 { return 0 } - return vals[i-1].v + return this.arr[index][i][1] } -type pair struct{ i, v int } - /** * Your SnapshotArray object will be instantiated and called as such: * obj := Constructor(length); diff --git a/solution/1100-1199/1146.Snapshot Array/Solution.java b/solution/1100-1199/1146.Snapshot Array/Solution.java index b1b55c1fb46df..380dc1be996db 100644 --- a/solution/1100-1199/1146.Snapshot Array/Solution.java +++ b/solution/1100-1199/1146.Snapshot Array/Solution.java @@ -16,17 +16,17 @@ public int snap() { } public int get(int index, int snap_id) { - var vals = arr[index]; - int left = 0, right = vals.size(); - while (left < right) { - int mid = (left + right) >> 1; - if (vals.get(mid)[0] > snap_id) { - right = mid; + int l = 0, r = arr[index].size(); + while (l < r) { + int mid = (l + r) >> 1; + if (arr[index].get(mid)[0] > snap_id) { + r = mid; } else { - left = mid + 1; + l = mid + 1; } } - return left == 0 ? 0 : vals.get(left - 1)[1]; + --l; + return l < 0 ? 0 : arr[index].get(l)[1]; } } diff --git a/solution/1100-1199/1146.Snapshot Array/Solution.py b/solution/1100-1199/1146.Snapshot Array/Solution.py index 5f5034fbed46f..a6dcd460d0987 100644 --- a/solution/1100-1199/1146.Snapshot Array/Solution.py +++ b/solution/1100-1199/1146.Snapshot Array/Solution.py @@ -1,19 +1,19 @@ class SnapshotArray: + def __init__(self, length: int): - self.idx = 0 - self.arr = defaultdict(list) + self.arr = [[] for _ in range(length)] + self.i = 0 def set(self, index: int, val: int) -> None: - self.arr[index].append((self.idx, val)) + self.arr[index].append((self.i, val)) def snap(self) -> int: - self.idx += 1 - return self.idx - 1 + self.i += 1 + return self.i - 1 def get(self, index: int, snap_id: int) -> int: - vals = self.arr[index] - i = bisect_right(vals, (snap_id, inf)) - 1 - return 0 if i < 0 else vals[i][1] + i = bisect_left(self.arr[index], (snap_id, inf)) - 1 + return 0 if i < 0 else self.arr[index][i][1] # Your SnapshotArray object will be instantiated and called as such: diff --git a/solution/1100-1199/1146.Snapshot Array/Solution.ts b/solution/1100-1199/1146.Snapshot Array/Solution.ts new file mode 100644 index 0000000000000..6673db8622ef9 --- /dev/null +++ b/solution/1100-1199/1146.Snapshot Array/Solution.ts @@ -0,0 +1,37 @@ +class SnapshotArray { + private arr: [number, number][][]; + private i: number = 0; + constructor(length: number) { + this.arr = Array.from({ length }, () => []); + } + + set(index: number, val: number): void { + this.arr[index].push([this.i, val]); + } + + snap(): number { + return this.i++; + } + + get(index: number, snap_id: number): number { + let [l, r] = [0, this.arr[index].length]; + while (l < r) { + const mid = (l + r) >> 1; + if (this.arr[index][mid][0] > snap_id) { + r = mid; + } else { + l = mid + 1; + } + } + --l; + return l < 0 ? 0 : this.arr[index][l][1]; + } +} + +/** + * Your SnapshotArray object will be instantiated and called as such: + * var obj = new SnapshotArray(length) + * obj.set(index,val) + * var param_2 = obj.snap() + * var param_3 = obj.get(index,snap_id) + */ diff --git a/solution/2600-2699/2639.Find the Width of Columns of a Grid/README.md b/solution/2600-2699/2639.Find the Width of Columns of a Grid/README.md index 6ec652126f29c..b8616fda88029 100644 --- a/solution/2600-2699/2639.Find the Width of Columns of a Grid/README.md +++ b/solution/2600-2699/2639.Find the Width of Columns of a Grid/README.md @@ -52,9 +52,9 @@ ### 方法一:模拟 -我们记矩阵的列数为 $n$,创建一个长度为 $n$ 的数组 $ans$,其中 $ans[i]$ 表示第 $i$ 列的宽度。初始时,$ans[i]$ 的值均为 $0$。 +我们记矩阵的列数为 $n$,创建一个长度为 $n$ 的数组 $ans$,其中 $ans[i]$ 表示第 $i$ 列的宽度。初始时 $ans[i] = 0$。 -遍历矩阵中的每一行,对于每一行中的每个元素,计算其字符串长度 $w$,并更新 $ans[j]$ 的值为 $max(ans[j], w)$。 +遍历矩阵中的每一行,对于每一行中的每个元素,计算其字符串长度 $w$,并更新 $ans[j]$ 的值为 $\max(ans[j], w)$。 遍历完所有行后,数组 $ans$ 中的每个元素即为对应列的宽度。 @@ -65,12 +65,7 @@ ```python class Solution: def findColumnWidth(self, grid: List[List[int]]) -> List[int]: - ans = [0] * len(grid[0]) - for row in grid: - for j, x in enumerate(row): - w = len(str(x)) - ans[j] = max(ans[j], w) - return ans + return [max(len(str(x)) for x in col) for col in zip(*grid)] ``` ```java diff --git a/solution/2600-2699/2639.Find the Width of Columns of a Grid/README_EN.md b/solution/2600-2699/2639.Find the Width of Columns of a Grid/README_EN.md index 26895c814fc78..b17ca20d4518e 100644 --- a/solution/2600-2699/2639.Find the Width of Columns of a Grid/README_EN.md +++ b/solution/2600-2699/2639.Find the Width of Columns of a Grid/README_EN.md @@ -48,19 +48,22 @@ In the 2nd column, both 12 and -2 are of length 2. ## Solutions -### Solution 1 +### Solution 1: Simulation + +We denote the number of columns in the matrix as $n$, and create an array $ans$ of length $n$, where $ans[i]$ represents the width of the $i$-th column. Initially, $ans[i] = 0$. + +We traverse each row in the matrix. For each element in each row, we calculate its string length $w$, and update the value of $ans[j]$ to be $\max(ans[j], w)$. + +After traversing all rows, each element in the array $ans$ is the width of the corresponding column. + +The time complexity is $O(m \times n)$, and the space complexity is $O(\log M)$. Where $m$ and $n$ are the number of rows and columns in the matrix respectively, and $M$ is the absolute value of the maximum element in the matrix. ```python class Solution: def findColumnWidth(self, grid: List[List[int]]) -> List[int]: - ans = [0] * len(grid[0]) - for row in grid: - for j, x in enumerate(row): - w = len(str(x)) - ans[j] = max(ans[j], w) - return ans + return [max(len(str(x)) for x in col) for col in zip(*grid)] ``` ```java diff --git a/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.py b/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.py index 5c7cdf5986e84..c8b6b486c2720 100644 --- a/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.py +++ b/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution.py @@ -1,8 +1,3 @@ class Solution: def findColumnWidth(self, grid: List[List[int]]) -> List[int]: - ans = [0] * len(grid[0]) - for row in grid: - for j, x in enumerate(row): - w = len(str(x)) - ans[j] = max(ans[j], w) - return ans + return [max(len(str(x)) for x in col) for col in zip(*grid)] diff --git a/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution2.py b/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution2.py deleted file mode 100644 index c8b6b486c2720..0000000000000 --- a/solution/2600-2699/2639.Find the Width of Columns of a Grid/Solution2.py +++ /dev/null @@ -1,3 +0,0 @@ -class Solution: - def findColumnWidth(self, grid: List[List[int]]) -> List[int]: - return [max(len(str(x)) for x in col) for col in zip(*grid)] diff --git a/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README.md b/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README.md index 72d5bedcb8501..53234daf16a37 100644 --- a/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README.md +++ b/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README.md @@ -60,7 +60,7 @@ 我们用变量 $mx$ 记录数组 $nums$ 中前 $i$ 个元素的最大值,用数组 $ans[i]$ 记录数组 $nums$ 中前 $i$ 个元素的分数。 -接下来,遍历数组 $nums$,对于每个元素 $nums[i]$,我们更新 $mx$,即 $mx = max(mx, nums[i])$,然后更新 $ans[i]$,如果 $i = 0$,则 $ans[i] = nums[i] + mx$,否则 $ans[i] = nums[i] + mx + ans[i - 1]$。 +接下来,遍历数组 $nums$,对于每个元素 $nums[i]$,我们更新 $mx$,即 $mx = \max(mx, nums[i])$,然后更新 $ans[i]$,如果 $i = 0$,则 $ans[i] = nums[i] + mx$,否则 $ans[i] = nums[i] + mx + ans[i - 1]$。 时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 diff --git a/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README_EN.md b/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README_EN.md index 1bff3df9d05b5..57dc865b6686e 100644 --- a/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README_EN.md +++ b/solution/2600-2699/2640.Find the Score of All Prefixes of an Array/README_EN.md @@ -54,7 +54,13 @@ For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] ## Solutions -### Solution 1 +### Solution 1: Prefix Sum + +We use a variable $mx$ to record the maximum value of the first $i$ elements in the array $nums$, and use an array $ans[i]$ to record the score of the first $i$ elements in the array $nums$. + +Next, we traverse the array $nums$. For each element $nums[i]$, we update $mx$, i.e., $mx = \max(mx, nums[i])$, and then update $ans[i]$. If $i = 0$, then $ans[i] = nums[i] + mx$, otherwise $ans[i] = nums[i] + mx + ans[i - 1]$. + +The time complexity is $O(n)$, where $n$ is the length of the array $nums$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.