forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
44 lines (33 loc) · 1.05 KB
/
main.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
/// Source : https://leetcode.com/problems/two-best-non-overlapping-events/
/// Author : liuyubobobo
/// Time : 2021-10-30
#include <iostream>
#include <vector>
using namespace std;
/// Binary Search
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class Solution {
public:
int maxTwoEvents(vector<vector<int>>& events) {
sort(events.begin(), events.end());
int n = events.size();
vector<int> max_value(n, events.back()[2]);
for(int i = n - 2; i >= 0; i --)
max_value[i] = max(events[i][2], max_value[i + 1]);
int res = 0;
for(int i = 0; i < n; i ++){
res = max(res, events[i][2]);
vector<int> t = {events[i][1] + 1, INT_MIN, INT_MIN};
vector<vector<int>>::iterator iter = lower_bound(events.begin(), events.end(), t);
if(iter != events.end()){
int index = iter - events.begin();
res = max(res, events[i][2] + max_value[index]);
}
}
return res;
}
};
int main() {
return 0;
}