-
Notifications
You must be signed in to change notification settings - Fork 0
/
1203.sort-items-by-groups-respecting-dependencies.2.cpp
93 lines (87 loc) · 2.18 KB
/
1203.sort-items-by-groups-respecting-dependencies.2.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @lc app=leetcode id=1203 lang=cpp
*
* [1203] Sort Items by Groups Respecting Dependencies
*/
// @lc code=start
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
vector<int> groupDegree;
int degree[30000];
vector<vector<int>> children;
vector<vector<int>> groupChildren;
vector<vector<int>> items;
public:
vector<int> sortItems(int n, int m, vector<int>& group, vector<vector<int>>& beforeItems) {
// init
children.resize(n);
items.resize(m);
for(int i = 0; i < n; ++i) {
if(group[i] == -1) {
group[i] = m++;
items.push_back({});
}
items[group[i]].push_back(i);
}
groupChildren.resize(m);
groupDegree.resize(m);
for(int i = 0; i < n; ++i) {
for(auto p : beforeItems[i]) {
children[p].push_back(i);
degree[i] += 1;
if(group[p] != group[i]) {
groupDegree[group[i]] += 1;
groupChildren[group[p]].push_back(group[i]);
}
}
}
// topological sort for group
vector<int> groupOrder;
queue<int> q;
for(int i = 0; i < m; ++i) {
if(!groupDegree[i]) q.push(i);
}
while(q.size()) {
int cur = q.front();
q.pop();
groupOrder.push_back(cur);
for(auto c : groupChildren[cur]) {
groupDegree[c] -= 1;
if(!groupDegree[c]) q.push(c);
}
}
// cout << groupOrder << endl;
if(groupOrder.size() != m) return {};
// sort items
vector<int> answer;
for(auto g : groupOrder) {
int sz = answer.size();
for(auto item : items[g]) {
if(!degree[item]) q.push(item);
}
while(q.size()) {
int cur = q.front();
q.pop();
answer.push_back(cur);
for(auto c : children[cur]) {
degree[c] -= 1;
if(group[c] == group[cur] && !degree[c]) {
q.push(c);
}
}
}
if(answer.size() - sz != items[g].size()) return {};
}
return answer;
}
};
// Accepted
// 17/17 cases passed (60 ms)
// Your runtime beats 97.68 % of cpp submissions
// Your memory usage beats 70.86 % of cpp submissions (45.2 MB)
// @lc code=end