Skip to content

Commit 77d9df2

Browse files
committed
feat: add ts solution to lc problem: No.2392
No.2392.Build a Matrix With Conditions
1 parent af8e3d8 commit 77d9df2

File tree

5 files changed

+144
-6
lines changed

5 files changed

+144
-6
lines changed

solution/2300-2399/2389.Longest Subsequence With Limited Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
`nums` 排序,对于每个 `queries[i]`,求出 `nums` 中所有元素之和小于等于 `queries[i]` 的子序列的最大长度。
5353

54-
时间复杂度 $O(n\log n + m\log n)$。其中 $n$ `nums` 的长度,$m$ `queries` 的长度。
54+
时间复杂度 $O((n+m)\times logn)$。其中 $n$ $m$ 分别为 `nums` `queries` 的长度。
5555

5656
<!-- tabs:start -->
5757

solution/2300-2399/2392.Build a Matrix With Conditions/README.md

+48-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171

7272
**方法一:拓扑排序**
7373

74-
利用拓扑排序,找到一个合法的 $row$ 序列和 $col$ 序列,然后根据这两个序列构造出矩阵。
74+
利用拓扑排序,找到一个合法的 `row` 序列和 `col` 序列,然后根据这两个序列构造出矩阵。
7575

76-
时间复杂度 $O(m+n+k^2)$。其中 $m$ 为 $rowConditions$ 的长度,$n$ 为 $colConditions$ 的长度,$k$ 为题目中的 $k$。
76+
时间复杂度 $O(m+n+k)$。其中 $m$ 和 $n$ 分别为 `rowConditions``colConditions` 的长度,$k$ 为题目中给定的正整数
7777

7878
<!-- tabs:start -->
7979

@@ -186,7 +186,7 @@ public:
186186
this->k = k;
187187
auto row = f(rowConditions);
188188
auto col = f(colConditions);
189-
if (row.empty() || col.empty()) return vector<vector<int>>();
189+
if (row.empty() || col.empty()) return {};
190190
vector<vector<int>> ans(k, vector<int>(k));
191191
vector<int> m(k + 1);
192192
for (int i = 0; i < k; ++i) {
@@ -291,7 +291,52 @@ func buildMatrix(k int, rowConditions [][]int, colConditions [][]int) [][]int {
291291
### **TypeScript**
292292

293293
```ts
294+
function buildMatrix(
295+
k: number,
296+
rowConditions: number[][],
297+
colConditions: number[][],
298+
): number[][] {
299+
function f(cond) {
300+
const g = Array.from({ length: k + 1 }, () => []);
301+
const indeg = new Array(k + 1).fill(0);
302+
for (const [a, b] of cond) {
303+
g[a].push(b);
304+
++indeg[b];
305+
}
306+
const q = [];
307+
for (let i = 1; i < indeg.length; ++i) {
308+
if (indeg[i] == 0) {
309+
q.push(i);
310+
}
311+
}
312+
const res = [];
313+
while (q.length) {
314+
for (let n = q.length; n; --n) {
315+
const i = q.shift();
316+
res.push(i);
317+
for (const j of g[i]) {
318+
if (--indeg[j] == 0) {
319+
q.push(j);
320+
}
321+
}
322+
}
323+
}
324+
return res.length == k ? res : [];
325+
}
294326

327+
const row = f(rowConditions);
328+
const col = f(colConditions);
329+
if (!row.length || !col.length) return [];
330+
const ans = Array.from({ length: k }, () => new Array(k).fill(0));
331+
const m = new Array(k + 1).fill(0);
332+
for (let i = 0; i < k; ++i) {
333+
m[col[i]] = i;
334+
}
335+
for (let i = 0; i < k; ++i) {
336+
ans[i][m[row[i]]] = row[i];
337+
}
338+
return ans;
339+
}
295340
```
296341

297342
### **...**

solution/2300-2399/2392.Build a Matrix With Conditions/README_EN.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ No matrix can satisfy all the conditions, so we return the empty matrix.
6363

6464
## Solutions
6565

66+
Topological Sort.
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
@@ -170,7 +172,7 @@ public:
170172
this->k = k;
171173
auto row = f(rowConditions);
172174
auto col = f(colConditions);
173-
if (row.empty() || col.empty()) return vector<vector<int>>();
175+
if (row.empty() || col.empty()) return {};
174176
vector<vector<int>> ans(k, vector<int>(k));
175177
vector<int> m(k + 1);
176178
for (int i = 0; i < k; ++i) {
@@ -275,7 +277,52 @@ func buildMatrix(k int, rowConditions [][]int, colConditions [][]int) [][]int {
275277
### **TypeScript**
276278

277279
```ts
280+
function buildMatrix(
281+
k: number,
282+
rowConditions: number[][],
283+
colConditions: number[][],
284+
): number[][] {
285+
function f(cond) {
286+
const g = Array.from({ length: k + 1 }, () => []);
287+
const indeg = new Array(k + 1).fill(0);
288+
for (const [a, b] of cond) {
289+
g[a].push(b);
290+
++indeg[b];
291+
}
292+
const q = [];
293+
for (let i = 1; i < indeg.length; ++i) {
294+
if (indeg[i] == 0) {
295+
q.push(i);
296+
}
297+
}
298+
const res = [];
299+
while (q.length) {
300+
for (let n = q.length; n; --n) {
301+
const i = q.shift();
302+
res.push(i);
303+
for (const j of g[i]) {
304+
if (--indeg[j] == 0) {
305+
q.push(j);
306+
}
307+
}
308+
}
309+
}
310+
return res.length == k ? res : [];
311+
}
278312

313+
const row = f(rowConditions);
314+
const col = f(colConditions);
315+
if (!row.length || !col.length) return [];
316+
const ans = Array.from({ length: k }, () => new Array(k).fill(0));
317+
const m = new Array(k + 1).fill(0);
318+
for (let i = 0; i < k; ++i) {
319+
m[col[i]] = i;
320+
}
321+
for (let i = 0; i < k; ++i) {
322+
ans[i][m[row[i]]] = row[i];
323+
}
324+
return ans;
325+
}
279326
```
280327

281328
### **...**

solution/2300-2399/2392.Build a Matrix With Conditions/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Solution {
66
this->k = k;
77
auto row = f(rowConditions);
88
auto col = f(colConditions);
9-
if (row.empty() || col.empty()) return vector<vector<int>>();
9+
if (row.empty() || col.empty()) return {};
1010
vector<vector<int>> ans(k, vector<int>(k));
1111
vector<int> m(k + 1);
1212
for (int i = 0; i < k; ++i) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function buildMatrix(
2+
k: number,
3+
rowConditions: number[][],
4+
colConditions: number[][],
5+
): number[][] {
6+
function f(cond) {
7+
const g = Array.from({ length: k + 1 }, () => []);
8+
const indeg = new Array(k + 1).fill(0);
9+
for (const [a, b] of cond) {
10+
g[a].push(b);
11+
++indeg[b];
12+
}
13+
const q = [];
14+
for (let i = 1; i < indeg.length; ++i) {
15+
if (indeg[i] == 0) {
16+
q.push(i);
17+
}
18+
}
19+
const res = [];
20+
while (q.length) {
21+
for (let n = q.length; n; --n) {
22+
const i = q.shift();
23+
res.push(i);
24+
for (const j of g[i]) {
25+
if (--indeg[j] == 0) {
26+
q.push(j);
27+
}
28+
}
29+
}
30+
}
31+
return res.length == k ? res : [];
32+
}
33+
34+
const row = f(rowConditions);
35+
const col = f(colConditions);
36+
if (!row.length || !col.length) return [];
37+
const ans = Array.from({ length: k }, () => new Array(k).fill(0));
38+
const m = new Array(k + 1).fill(0);
39+
for (let i = 0; i < k; ++i) {
40+
m[col[i]] = i;
41+
}
42+
for (let i = 0; i < k; ++i) {
43+
ans[i][m[row[i]]] = row[i];
44+
}
45+
return ans;
46+
}

0 commit comments

Comments
 (0)