Skip to content

Commit ac93bdf

Browse files
committed
feat: add solutions to lc problems: No.1424,1425
* No.1424.Diagonal Traverse II * No.1425.Constrained Subsequence Sum
1 parent 074b513 commit ac93bdf

File tree

10 files changed

+740
-115
lines changed

10 files changed

+740
-115
lines changed

solution/1400-1499/1424.Diagonal Traverse II/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ tags:
7777
- 下一条对角线的 $i + j$ 的值比前一条对角线的大;
7878
- 在同一条对角线中的 $i + j$ 是相同的,而 $j$ 值是从小到大递增。
7979

80-
因此,我们将所有数字以 `(i + j, j, nums[i][j])` 的形式存进 `arr`,然后按照前两项排序。最后返回 `arr` 所有元素第二项组成的数组即可
80+
因此,我们将所有数字以 $(i, j, \textit{nums}[i][j])$ 的形式存进 $\textit{arr}$,然后按照前两项排序。最后返回 $\textit{arr}$ 所有元素下标为 $2$ 的值组成的数组即可
8181

82-
时间复杂度 $O(n\log n)$,其中 $n$ `nums` 数组元素的个数
82+
时间复杂度 $O(n \times \log n)$,其中 $n$ 是数组 $\textit{nums}$ 中元素的个数。空间复杂度 $O(n)$
8383

8484
<!-- tabs:start -->
8585

@@ -163,6 +163,21 @@ func findDiagonalOrder(nums [][]int) []int {
163163
}
164164
```
165165

166+
#### TypeScript
167+
168+
```ts
169+
function findDiagonalOrder(nums: number[][]): number[] {
170+
const arr: number[][] = [];
171+
for (let i = 0; i < nums.length; ++i) {
172+
for (let j = 0; j < nums[i].length; ++j) {
173+
arr.push([i + j, j, nums[i][j]]);
174+
}
175+
}
176+
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
177+
return arr.map(x => x[2]);
178+
}
179+
```
180+
166181
#### C#
167182

168183
```cs

solution/1400-1499/1424.Diagonal Traverse II/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Sorting
57+
58+
We observe that:
59+
60+
- The value of $i + j$ is the same for each diagonal;
61+
- The value of $i + j$ for the next diagonal is greater than that of the previous diagonal;
62+
- Within the same diagonal, the value of $i + j$ is the same, and the value of $j$ increases from small to large.
63+
64+
Therefore, we store all numbers in the form of $(i, j, \textit{nums}[i][j])$ into $\textit{arr}$, and then sort according to the first two items. Finally, return the array composed of the values at index 2 of all elements in $\textit{arr}$.
65+
66+
The time complexity is $O(n \times \log n)$, where $n$ is the number of elements in the array $\textit{nums}$. The space complexity is $O(n)$.
5767

5868
<!-- tabs:start -->
5969

@@ -137,6 +147,21 @@ func findDiagonalOrder(nums [][]int) []int {
137147
}
138148
```
139149

150+
#### TypeScript
151+
152+
```ts
153+
function findDiagonalOrder(nums: number[][]): number[] {
154+
const arr: number[][] = [];
155+
for (let i = 0; i < nums.length; ++i) {
156+
for (let j = 0; j < nums[i].length; ++j) {
157+
arr.push([i + j, j, nums[i][j]]);
158+
}
159+
}
160+
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
161+
return arr.map(x => x[2]);
162+
}
163+
```
164+
140165
#### C#
141166

142167
```cs
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function findDiagonalOrder(nums: number[][]): number[] {
2+
const arr: number[][] = [];
3+
for (let i = 0; i < nums.length; ++i) {
4+
for (let j = 0; j < nums[i].length; ++j) {
5+
arr.push([i + j, j, nums[i][j]]);
6+
}
7+
}
8+
arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
9+
return arr.map(x => x[2]);
10+
}

0 commit comments

Comments
 (0)