generated from projeto-de-algoritmos/RepositorioTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1235.cpp
46 lines (40 loc) · 1.21 KB
/
1235.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
class Solution {
public:
struct Interval{
int start, finish, weight;
};
int binarySearch(vector<Interval> &intervals, int j) {
int start = 0;
int end = j - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (intervals[mid].finish <= intervals[j].start) {
if (intervals[mid + 1].finish <= intervals[j].start) {
start = mid + 1;
} else {
return mid;
}
} else {
end = mid - 1;
}
}
return -1;
}
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit){
int n = startTime.size();
vector<Interval> intervals(n);
for(int i = 0; i<n;i++){
intervals[i]={startTime[i],endTime[i],profit[i]};
}
vector<int> M(n+1,0);
vector<int> p(n+1,0);
sort(intervals.begin(), intervals.end(),
[](const Interval &a, const Interval b) { return a.finish < b.finish; });
for (int j = 1; j <= n; ++j) {
int w_j = intervals[j - 1].weight;
p[j] = binarySearch(intervals, j - 1) + 1;
M[j] = max(w_j + M[p[j]], M[j - 1]);
}
return M[n];
}
};