Skip to content

Commit b65406c

Browse files
committed
feat: update solutions to lc problems: No.1494,1496
* No.1494.Parallel Courses II * No.1496.Path Crossing
1 parent 9324b58 commit b65406c

File tree

6 files changed

+122
-52
lines changed

6 files changed

+122
-52
lines changed

solution/1400-1499/1494.Parallel Courses II/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63-
**方法一:状态压缩 + BFS + 枚举子集**
63+
**方法一:状态压缩 + BFS + 子集枚举**
6464

65-
我们用数组 $d[i]$ 表示课程 $i$ 的先修课程的集合。由于数据规模 $n\lt 15$,我们可以用一个整数的二进制位来表示集合,其中第 $j$ 位为 $1$ 表示课程 $j$ 是课程 $i$ 的先修课程。
65+
我们用数组 $d[i]$ 表示课程 $i$ 的先修课程的集合。由于数据规模 $n\lt 15$,我们可以用一个整数的二进制位(状态压缩)来表示集合,其中第 $j$ 位为 $1$ 表示课程 $j$ 是课程 $i$ 的先修课程。
6666

6767
我们用一个状态变量 $cur$ 表示当前已经上过的课程的集合,初始时 $cur=0$。如果 $cur=2^{n+1}-2$,表示所有课程都上过了,返回当前学期即可。
6868

6969
如果课程 $i$ 的先修课程 $d[i]$ 的集合是 $cur$ 的子集,说明课程 $i$ 可以上。这样我们可以找到当前 $cur$ 状态的下一个状态 $nxt$,表示后续学期可以上的课程集合。
7070

7171
如果 $nxt$ 的二进制表示中 $1$ 的个数小于等于 $k$,说明后续学期可以上的课程数不超过 $k$,我们就可以将 $nxt$ 加入队列中。否则,说明后续学期可以上的课程数超过 $k$,那么我们就应该从后续可以上的课程中选择 $k$ 门课程,这样才能保证后续学期可以上的课程数不超过 $k$。我们可以枚举 $nxt$ 的所有子集,将子集加入队列中。
7272

73-
时间复杂度 $O(2^n\times n)$,空间复杂度 $O(2^n\times)$
73+
时间复杂度 $O(3^n)$,空间复杂度 $O(2^n)$。其中 $n$ 是题目给定的课程数
7474

7575
<!-- tabs:start -->
7676

@@ -158,16 +158,14 @@ class Solution {
158158
### **C++**
159159

160160
```cpp
161-
using pii = pair<int, int>;
162-
163161
class Solution {
164162
public:
165163
int minNumberOfSemesters(int n, vector<vector<int>>& relations, int k) {
166164
vector<int> d(n + 1);
167165
for (auto& e : relations) {
168166
d[e[1]] |= 1 << e[0];
169167
}
170-
queue<pii> q;
168+
queue<pair<int, int>> q;
171169
q.push({0, 0});
172170
unordered_set<int> vis{{0}};
173171
while (!q.empty()) {

solution/1400-1499/1494.Parallel Courses II/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,14 @@ class Solution {
132132
### **C++**
133133

134134
```cpp
135-
using pii = pair<int, int>;
136-
137135
class Solution {
138136
public:
139137
int minNumberOfSemesters(int n, vector<vector<int>>& relations, int k) {
140138
vector<int> d(n + 1);
141139
for (auto& e : relations) {
142140
d[e[1]] |= 1 << e[0];
143141
}
144-
queue<pii> q;
142+
queue<pair<int, int>> q;
145143
q.push({0, 0});
146144
unordered_set<int> vis{{0}};
147145
while (!q.empty()) {
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
using pii = pair<int, int>;
2-
3-
class Solution {
4-
public:
5-
int minNumberOfSemesters(int n, vector<vector<int>>& relations, int k) {
6-
vector<int> d(n + 1);
7-
for (auto& e : relations) {
8-
d[e[1]] |= 1 << e[0];
9-
}
10-
queue<pii> q;
11-
q.push({0, 0});
12-
unordered_set<int> vis{{0}};
13-
while (!q.empty()) {
14-
auto [cur, t] = q.front();
15-
q.pop();
16-
if (cur == (1 << (n + 1)) - 2) {
17-
return t;
18-
}
19-
int nxt = 0;
20-
for (int i = 1; i <= n; ++i) {
21-
if ((cur & d[i]) == d[i]) {
22-
nxt |= 1 << i;
23-
}
24-
}
25-
nxt ^= cur;
26-
if (__builtin_popcount(nxt) <= k) {
27-
if (!vis.count(nxt | cur)) {
28-
vis.insert(nxt | cur);
29-
q.push({nxt | cur, t + 1});
30-
}
31-
} else {
32-
int x = nxt;
33-
while (nxt) {
34-
if (__builtin_popcount(nxt) == k && !vis.count(nxt | cur)) {
35-
vis.insert(nxt | cur);
36-
q.push({nxt | cur, t + 1});
37-
}
38-
nxt = (nxt - 1) & x;
39-
}
40-
}
41-
}
42-
return 0;
43-
}
1+
class Solution {
2+
public:
3+
int minNumberOfSemesters(int n, vector<vector<int>>& relations, int k) {
4+
vector<int> d(n + 1);
5+
for (auto& e : relations) {
6+
d[e[1]] |= 1 << e[0];
7+
}
8+
queue<pair<int, int>> q;
9+
q.push({0, 0});
10+
unordered_set<int> vis{{0}};
11+
while (!q.empty()) {
12+
auto [cur, t] = q.front();
13+
q.pop();
14+
if (cur == (1 << (n + 1)) - 2) {
15+
return t;
16+
}
17+
int nxt = 0;
18+
for (int i = 1; i <= n; ++i) {
19+
if ((cur & d[i]) == d[i]) {
20+
nxt |= 1 << i;
21+
}
22+
}
23+
nxt ^= cur;
24+
if (__builtin_popcount(nxt) <= k) {
25+
if (!vis.count(nxt | cur)) {
26+
vis.insert(nxt | cur);
27+
q.push({nxt | cur, t + 1});
28+
}
29+
} else {
30+
int x = nxt;
31+
while (nxt) {
32+
if (__builtin_popcount(nxt) == k && !vis.count(nxt | cur)) {
33+
vis.insert(nxt | cur);
34+
q.push({nxt | cur, t + 1});
35+
}
36+
nxt = (nxt - 1) & x;
37+
}
38+
}
39+
}
40+
return 0;
41+
}
4442
};

solution/1400-1499/1496.Path Crossing/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ func isPathCrossing(path string) bool {
164164
}
165165
```
166166

167+
### **TypeScript**
168+
169+
```ts
170+
function isPathCrossing(path: string): boolean {
171+
let [i, j] = [0, 0];
172+
const vis: Set<number> = new Set();
173+
vis.add(0);
174+
for (const c of path) {
175+
if (c === 'N') {
176+
--i;
177+
} else if (c === 'S') {
178+
++i;
179+
} else if (c === 'E') {
180+
++j;
181+
} else if (c === 'W') {
182+
--j;
183+
}
184+
const t = i * 20000 + j;
185+
if (vis.has(t)) {
186+
return true;
187+
}
188+
vis.add(t);
189+
}
190+
return false;
191+
}
192+
```
193+
167194
### **...**
168195

169196
```

solution/1400-1499/1496.Path Crossing/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,33 @@ func isPathCrossing(path string) bool {
139139
}
140140
```
141141

142+
### **TypeScript**
143+
144+
```ts
145+
function isPathCrossing(path: string): boolean {
146+
let [i, j] = [0, 0];
147+
const vis: Set<number> = new Set();
148+
vis.add(0);
149+
for (const c of path) {
150+
if (c === 'N') {
151+
--i;
152+
} else if (c === 'S') {
153+
++i;
154+
} else if (c === 'E') {
155+
++j;
156+
} else if (c === 'W') {
157+
--j;
158+
}
159+
const t = i * 20000 + j;
160+
if (vis.has(t)) {
161+
return true;
162+
}
163+
vis.add(t);
164+
}
165+
return false;
166+
}
167+
```
168+
142169
### **...**
143170

144171
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function isPathCrossing(path: string): boolean {
2+
let [i, j] = [0, 0];
3+
const vis: Set<number> = new Set();
4+
vis.add(0);
5+
for (const c of path) {
6+
if (c === 'N') {
7+
--i;
8+
} else if (c === 'S') {
9+
++i;
10+
} else if (c === 'E') {
11+
++j;
12+
} else if (c === 'W') {
13+
--j;
14+
}
15+
const t = i * 20000 + j;
16+
if (vis.has(t)) {
17+
return true;
18+
}
19+
vis.add(t);
20+
}
21+
return false;
22+
}

0 commit comments

Comments
 (0)