Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a20be2

Browse files
committed
feat: add solutions to lc problem: No.1462
No.1462.Course Schedule IV
1 parent 10cc2d2 commit 5a20be2

File tree

7 files changed

+433
-25
lines changed

7 files changed

+433
-25
lines changed

‎solution/0400-0499/0444.Sequence Reconstruction/README_EN.md‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,68 @@
44

55
## Description
66

7-
<p>Check whether the original sequence <code>org</code> can be uniquely reconstructed from the sequences in <code>seqs</code>. The <code>org</code> sequence is a permutation of the integers from 1 to n, with 1 &le; n &le; 10<sup>4</sup>. Reconstruction means building a shortest common supersequence of the sequences in <code>seqs</code> (i.e., a shortest sequence so that all sequences in <code>seqs</code> are subsequences of it). Determine whether there is only one sequence that can be reconstructed from <code>seqs</code> and it is the <code>org</code> sequence.</p>
7+
<p>You are given an integer array <code>nums</code> of length <code>n</code> where <code>nums</code> is a permutation of the integers in the range <code>[1, n]</code>. You are also given a 2D integer array <code>sequences</code> where <code>sequences[i]</code> is a subsequence of <code>nums</code>.</p>
8+
9+
<p>Check if <code>nums</code> is the shortest possible and the only <strong>supersequence</strong>. The shortest <strong>supersequence</strong> is a sequence <strong>with the shortest length</strong> and has all <code>sequences[i]</code> as subsequences. There could be multiple valid <strong>supersequences</strong> for the given array <code>sequences</code>.</p>
10+
11+
<ul>
12+
<li>For example, for <code>sequences = [[1,2],[1,3]]</code>, there are two shortest <strong>supersequences</strong>, <code>[1,2,3]</code> and <code>[1,3,2]</code>.</li>
13+
<li>While for <code>sequences = [[1,2],[1,3],[1,2,3]]</code>, the only shortest <strong>supersequence</strong> possible is <code>[1,2,3]</code>. <code>[1,2,3,4]</code> is a possible supersequence but not the shortest.</li>
14+
</ul>
15+
16+
<p>Return <code>true</code><em> if </em><code>nums</code><em> is the only shortest <strong>supersequence</strong> for </em><code>sequences</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
17+
18+
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
819

920
<p>&nbsp;</p>
1021
<p><strong>Example 1:</strong></p>
1122

1223
<pre>
13-
<strong>Input:</strong> org = [1,2,3], seqs = [[1,2],[1,3]]
24+
<strong>Input:</strong> nums = [1,2,3], sequences = [[1,2],[1,3]]
1425
<strong>Output:</strong> false
15-
<strong>Explanation:</strong> [1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.
26+
<strong>Explanation:</strong> There are two possible supersequences: [1,2,3] and [1,3,2].
27+
The sequence [1,2] is a subsequence of both: [<strong><u>1</u></strong>,<strong><u>2</u></strong>,3] and [<strong><u>1</u></strong>,3,<strong><u>2</u></strong>].
28+
The sequence [1,3] is a subsequence of both: [<strong><u>1</u></strong>,2,<strong><u>3</u></strong>] and [<strong><u>1</u></strong>,<strong><u>3</u></strong>,2].
29+
Since nums is not the only shortest supersequence, we return false.
1630
</pre>
1731

1832
<p><strong>Example 2:</strong></p>
1933

2034
<pre>
21-
<strong>Input:</strong> org = [1,2,3], seqs = [[1,2]]
35+
<strong>Input:</strong> nums = [1,2,3], sequences = [[1,2]]
2236
<strong>Output:</strong> false
23-
<strong>Explanation:</strong> The reconstructed sequence can only be [1,2].
37+
<strong>Explanation:</strong> The shortest possible supersequence is [1,2].
38+
The sequence [1,2] is a subsequence of it: [<strong><u>1</u></strong>,<strong><u>2</u></strong>].
39+
Since nums is not the shortest supersequence, we return false.
2440
</pre>
2541

2642
<p><strong>Example 3:</strong></p>
2743

2844
<pre>
29-
<strong>Input:</strong> org = [1,2,3], seqs = [[1,2],[1,3],[2,3]]
30-
<strong>Output:</strong> true
31-
<strong>Explanation:</strong> The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3].
32-
</pre>
33-
34-
<p><strong>Example 4:</strong></p>
35-
36-
<pre>
37-
<strong>Input:</strong> org = [4,1,5,2,6,3], seqs = [[5,2,6,3],[4,1,5,2]]
45+
<strong>Input:</strong> nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
3846
<strong>Output:</strong> true
47+
<strong>Explanation:</strong> The shortest possible supersequence is [1,2,3].
48+
The sequence [1,2] is a subsequence of it: [<strong><u>1</u></strong>,<strong><u>2</u></strong>,3].
49+
The sequence [1,3] is a subsequence of it: [<strong><u>1</u></strong>,2,<strong><u>3</u></strong>].
50+
The sequence [2,3] is a subsequence of it: [1,<strong><u>2</u></strong>,<strong><u>3</u></strong>].
51+
Since nums is the only shortest supersequence, we return true.
3952
</pre>
4053

4154
<p>&nbsp;</p>
4255
<p><strong>Constraints:</strong></p>
4356

4457
<ul>
45-
<li><code>1 &lt;= n &lt;= 10^4</code></li>
46-
<li><code>org</code> is a permutation of {1,2,...,n}.</li>
47-
<li><code>1 &lt;= segs[i].length &lt;= 10^5</code></li>
48-
<li><code>seqs[i][j]</code>&nbsp;fits in a 32-bit signed integer.</li>
58+
<li><code>n == nums.length</code></li>
59+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
60+
<li><code>nums</code> is a permutation of all the integers in the range <code>[1, n]</code>.</li>
61+
<li><code>1 &lt;= sequences.length &lt;= 10<sup>4</sup></code></li>
62+
<li><code>1 &lt;= sequences[i].length &lt;= 10<sup>4</sup></code></li>
63+
<li><code>1 &lt;= sum(sequences[i].length) &lt;= 10<sup>5</sup></code></li>
64+
<li><code>1 &lt;= sequences[i][j] &lt;= n</code></li>
65+
<li>All the arrays of <code>sequences</code> are <strong>unique</strong>.</li>
66+
<li><code>sequences[i]</code> is a subsequence of <code>nums</code>.</li>
4967
</ul>
5068

51-
<p>&nbsp;</p>
52-
53-
<p><b><font color="red">UPDATE (2017年1月8日):</font></b><br />
54-
The <i>seqs</i> parameter had been changed to a list of list of strings (instead of a 2d array of strings). Please reload the code definition to get the latest changes.</p>
55-
5669
## Solutions
5770

5871
<!-- tabs:start -->

‎solution/1400-1499/1462.Course Schedule IV/README.md‎

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,157 @@
7575

7676
<!-- 这里可写通用的实现逻辑 -->
7777

78+
DFS 记忆化搜索。
79+
7880
<!-- tabs:start -->
7981

8082
### **Python3**
8183

8284
<!-- 这里可写当前语言的特殊实现逻辑 -->
8385

8486
```python
85-
87+
class Solution:
88+
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
89+
@lru_cache(None)
90+
def dfs(a, b):
91+
if b in g[a] or a == b:
92+
return True
93+
for c in g[a]:
94+
if dfs(c, b):
95+
return True
96+
return False
97+
98+
g = defaultdict(set)
99+
for a, b in prerequisites:
100+
g[a].add(b)
101+
return [dfs(a, b) for a, b in queries]
86102
```
87103

88104
### **Java**
89105

90106
<!-- 这里可写当前语言的特殊实现逻辑 -->
91107

92108
```java
109+
class Solution {
110+
public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
111+
int[][] g = new int[numCourses][numCourses];
112+
for (int i = 0; i < numCourses; ++i) {
113+
Arrays.fill(g[i], -1);
114+
}
115+
for (int[] e : prerequisites) {
116+
int a = e[0], b = e[1];
117+
g[a][b] = 1;
118+
}
119+
List<Boolean> ans = new ArrayList<>();
120+
for (int[] e : queries) {
121+
int a = e[0], b = e[1];
122+
ans.add(dfs(a, b, g));
123+
}
124+
return ans;
125+
}
126+
127+
private boolean dfs(int a, int b, int[][] g) {
128+
if (g[a][b] != -1) {
129+
return g[a][b] == 1;
130+
}
131+
if (a == b) {
132+
g[a][b] = 1;
133+
return true;
134+
}
135+
for (int i = 0; i < g[a].length; ++i) {
136+
if (g[a][i] == 1 && dfs(i, b, g)) {
137+
g[a][b] = 1;
138+
return true;
139+
}
140+
}
141+
g[a][b] = 0;
142+
return false;
143+
}
144+
}
145+
```
146+
147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
153+
vector<vector<int>> g(numCourses, vector<int>(numCourses, -1));
154+
for (auto& e : prerequisites)
155+
{
156+
int a = e[0], b = e[1];
157+
g[a][b] = 1;
158+
}
159+
vector<bool> ans;
160+
for (auto& e : queries)
161+
{
162+
int a = e[0], b = e[1];
163+
ans.push_back(dfs(a, b, g));
164+
}
165+
return ans;
166+
}
167+
168+
bool dfs(int a, int b, vector<vector<int>>& g) {
169+
if (g[a][b] != -1) return g[a][b] == 1;
170+
if (a == b)
171+
{
172+
g[a][b] = 1;
173+
return true;
174+
}
175+
for (int i = 0; i < g[a].size(); ++i)
176+
{
177+
if (g[a][i] == 1 && dfs(i, b, g))
178+
{
179+
g[a][b] = 1;
180+
return true;
181+
}
182+
}
183+
g[a][b] = 0;
184+
return false;
185+
}
186+
};
187+
```
93188

189+
### **Go**
190+
191+
```go
192+
func checkIfPrerequisite(numCourses int, prerequisites [][]int, queries [][]int) []bool {
193+
g := make([][]int, numCourses)
194+
for i := range g {
195+
g[i] = make([]int, numCourses)
196+
for j := range g[i] {
197+
g[i][j] = -1
198+
}
199+
}
200+
for _, e := range prerequisites {
201+
a, b := e[0], e[1]
202+
g[a][b] = 1
203+
}
204+
var ans []bool
205+
var dfs func(a, b int) bool
206+
dfs = func(a, b int) bool {
207+
if g[a][b] != -1 {
208+
return g[a][b] == 1
209+
}
210+
if a == b {
211+
g[a][b] = 1
212+
return true
213+
}
214+
for i, c := range g[a] {
215+
if c == 1 && dfs(i, b) {
216+
g[a][b] = 1
217+
return true
218+
}
219+
}
220+
g[a][b] = 0
221+
return false
222+
}
223+
for _, e := range queries {
224+
a, b := e[0], e[1]
225+
ans = append(ans, dfs(a, b))
226+
}
227+
return ans
228+
}
94229
```
95230

96231
### **...**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /