You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For every event, I've two options. Whether I choose that event or not.
6
+
If choose, then I add its value to my answer. If not choosen I move forward to the next event.
7
+
Now if I select a event, my next event has to have start time > end time of choosen event. Also of all the possible next events I should choose the one with max value
8
+
To select the next event with start time > end time of choosen event, I can do a Binary search.
9
+
What to select the Max value I can find the value of all legal subsets possible, and select the maximum of all the values.
10
+
*/
11
+
vector<int> start;
12
+
int dp[100002][2];
13
+
14
+
intsolve(vector<vector<int>>& events, int i, int choose){
15
+
if(i>=events.size()){
16
+
return0;
17
+
}
18
+
19
+
if(choose>=2){ // since I cannot chose more than two elemnts.
20
+
return0;
21
+
}
22
+
23
+
if(dp[i][choose]!=-1){
24
+
return dp[i][choose];
25
+
}
26
+
27
+
int idx = upper_bound(start.begin(),start.end(),events[i][1])-start.begin(); // binary search to find the next event's index
28
+
29
+
return dp[i][choose]=max(events[i][2]+solve(events,idx,choose+1),solve(events,i+1,choose)); //either I select an event, or I dont
0 commit comments