Skip to content

Commit ed1a1db

Browse files
committed
444 solved.
1 parent 1dbb556 commit ed1a1db

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0444)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0444 main.cpp)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// Source : https://leetcode.com/problems/sequence-reconstruction/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-22
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
12+
/// Topological Sort
13+
/// Time Complexity: O(n + m)
14+
/// Space Complexity: O(n + m)
15+
class Solution {
16+
public:
17+
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& sequences) {
18+
19+
int n = nums.size();
20+
21+
vector<vector<int>> g(n);
22+
for(const vector<int>& seq: sequences){
23+
for(int i = 1; i < seq.size(); i ++){
24+
int a = seq[i - 1] - 1, b = seq[i] - 1;
25+
g[a].push_back(b);
26+
}
27+
}
28+
29+
vector<int> indegrees(n, 0);
30+
for(int u = 0; u < n; u ++)
31+
for(int v: g[u]) indegrees[v] ++;
32+
33+
queue<int> q;
34+
for(int u = 0; u < n; u ++)
35+
if(indegrees[u] == 0) q.push(u);
36+
37+
vector<int> res;
38+
while(!q.empty()){
39+
if(q.size() > 1) return false;
40+
41+
int u = q.front(); q.pop();
42+
res.push_back(u + 1);
43+
for(int v: g[u]){
44+
indegrees[v] --;
45+
if(indegrees[v] == 0) q.push(v);
46+
}
47+
}
48+
49+
return res == nums;
50+
}
51+
};
52+
53+
54+
int main() {
55+
56+
return 0;
57+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
476476
| 441 | [Arranging Coins](https://leetcode.com/problems/arranging-coins/) | [solution](https://leetcode.com/problems/arranging-coins/solution/) | [C++](0001-0500/0441-Arranging-Coins/cpp-0441/) | | |
477477
| 442 | [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [solution](https://leetcode.com/problems/find-all-duplicates-in-an-array/solution/) | [C++](0001-0500/0442-Find-All-Duplicates-in-an-Array/cpp-0442/) | | |
478478
| 443 | [String Compression](https://leetcode.com/problems/string-compression/description/) | | [C++](0001-0500/0443-String-Compression/cpp-0443/) | | |
479-
| | | | | | |
479+
| 444 | [Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/) | [] | [C++](0001-0500/0444-Sequence-Reconstruction/cpp-0444/) | | |
480480
| 445 | [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [] | [C++](0001-0500/0445-Add-Two-Numbers-II/cpp-0445/) | | |
481481
| 446 | [Arithmetic Slices II - Subsequence](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/) | [solution](https://leetcode.com/problems/arithmetic-slices-ii-subsequence/solution/) | [C++](0001-0500/0446-Arithmetic-Slices-II-Subsequence/cpp-0446/) | | |
482482
| 447 | [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/description/) | [] | [C++](0001-0500/0447-Number-of-Boomerangs/cpp-0447/) | [Java](0001-0500/0447-Number-of-Boomerangs/java-0447/src/) | |

0 commit comments

Comments
 (0)