-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSequence_Reconstruction.cpp
34 lines (34 loc) · 1.06 KB
/
Sequence_Reconstruction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
if(seqs.empty()) return false;
int n = org.size();
vector<bool> pair(n, false);
vector<int> indx(n + 1);
for(int i = 0 ; i < n; ++i) {
indx[org[i]] = i;
}
bool isEmpty = true;
for(int i = 0; i < seqs.size(); ++i) {
for(int j= 0; j < seqs[i].size(); ++j) {
isEmpty = false;
if(seqs[i][j] > n or seqs[i][j] <= 0) {
return false;
}
if(j > 0 and indx[seqs[i][j - 1]] >= indx[seqs[i][j]]) {
return false;
}
if(j > 0 and indx[seqs[i][j - 1]] + 1 == indx[seqs[i][j]]) {
pair[indx[seqs[i][j - 1]]] = true;
}
}
}
for(int i = 0 ; i < n - 1; ++i) {
if(!pair[i]) {
return false;
}
}
if(n == 1 and isEmpty) return false;
return true;
}
};