forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sequence-reconstruction.cpp
36 lines (34 loc) · 1.09 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
35
36
// Time: O(n * s), n is the size of org, s is the size of seqs
// Space: O(n)
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
if (seqs.empty()) {
return false;
}
vector<int> pos(org.size() + 1);
for (int i = 0; i < org.size(); ++i) {
pos[org[i]] = i;
}
vector<bool> is_matched(org.size() + 1);
int cnt_to_match = org.size() - 1;
for (const auto& seq : seqs) {
for (int i = 0; i < seq.size(); ++i) {
if (seq[i] <= 0 || seq[i] > org.size()) {
return false;
}
if (i == 0) {
continue;
}
if (pos[seq[i - 1]] >= pos[seq[i]]) {
return false;
}
if (is_matched[seq[i - 1]] == false && pos[seq[i - 1]] + 1 == pos[seq[i]]) {
is_matched[seq[i - 1]] = true;
--cnt_to_match;
}
}
}
return cnt_to_match == 0;
}
};