-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1494. Parallel Courses II.cpp
130 lines (114 loc) · 3.98 KB
/
1494. Parallel Courses II.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//WA
//29 / 53 test cases passed.
class Solution {
public:
int minNumberOfSemesters(int n, vector<vector<int>>& dependencies, int k) {
vector<bool> visited(n+1, false);
set<int> sources;
vector<vector<int>> edges(n+1);
set<int>::iterator it;
for(int i = 1; i <= n; ++i){
sources.insert(i);
}
for(vector<int> dep : dependencies){
if((it = sources.find(dep[1])) != sources.end()){
sources.erase(it);
}
edges[dep[0]].push_back(dep[1]);
}
/*
cout << "sources: ";
for(const int& source : sources){
cout << source << " ";
}
cout << endl;
*/
int ans = 0;
set<int> next_sources;
while(!sources.empty()){
/*
cout << "sources: ";
for(const int& source : sources){
cout << source << " ";
}
cout << endl;
cout << "add: " << ceil((double)sources.size()/k) << endl;
*/
ans += ceil((double)sources.size()/k);
for(int source : sources){
next_sources.insert(edges[source].begin(), edges[source].end());
}
swap(sources, next_sources);
next_sources.clear();
}
return ans;
}
};
//dp, bitmask
//https://leetcode.com/problems/parallel-courses-ii/discuss/708263/Can-anyone-explain-the-bit-mask-method
//Runtime: 440 ms, faster than 13.11% of C++ online submissions for Parallel Courses II.
//Memory Usage: 8.3 MB, less than 83.33% of C++ online submissions for Parallel Courses II.
class Solution {
public:
int minNumberOfSemesters(int n, vector<vector<int>>& dependencies, int k) {
/*
prerequisite of n courses
if course i is dependent on course j,
then pre[i]'s jth bit will be set
*/
vector<int> pre(n);
for(vector<int>& dep : dependencies){
pre[dep[1]-1] |= (1 << (dep[0]-1));
}
/*
key: the combination of courses(bit representation)
value: minimum days to take all the courses
*/
vector<int> dp(1<<n, n);
//require 0 days to take 0 courses
dp[0] = 0;
/*
need to go into the loop even if cur is 0,
because in there we will update dp[cur|subnext]!
*/
for(int cur = 0; cur < (1<<n); ++cur){
/*
if we have taken all courses specified by "cur",
we can take the courses specified by "next"
*/
int next = 0;
for(int j = 0; j < n; ++j){
/*
if we have taken all prerequisites of course "j",
we can then take course "j" now
*/
if((cur & pre[j]) == pre[j]){
/*
cur is the superset of pre[j],
that means in current state,
we have taken all prerequisites of course j
*/
next |= (1<<j);
}
}
/*
now "next" specify the course we are able to taken,
but we don't need to take the courses that are already taken
(which are specified by cur)
so ""&= ~cur" to exclude those courses
*/
next &= ~cur;
/*
https://cp-algorithms.com/algebra/all-submasks.html
enumerate all the bit 1 combinations(submask)?
*/
for(int subnext = next; subnext; subnext = (subnext-1) & next){
if(__builtin_popcount(subnext) <= k){
//we can take at most k courses in a day
dp[cur|subnext] = min(dp[cur|subnext], dp[cur]+1);
}
}
}
return dp.back();
}
};