-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMaximumNumberOfEventsThatCanBeAttended2.java
40 lines (36 loc) · 1.26 KB
/
MaximumNumberOfEventsThatCanBeAttended2.java
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
/*https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended-ii/*/
class Solution {
int n;
Integer[][] values;
public int maxValue(int[][] events, int k) {
Arrays.sort(events,(a,b)->(a[0] == b[0] ? (a[1] == b[1] ? b[2]-a[2] : a[1]-b[1]) : a[0]-b[0]));
n = events.length;
values = new Integer[n+1][k+1];
values[0][k] = count(events, k, 0);
return values[0][k];
}
private int count(int[][] events, int k, int index)
{
if (k == 0 || index == n || index == -1) return 0;
if (values[index][k] != null) return values[index][k];
int result = 0;
result = Math.max(result,count(events, k, index+1));
if (k > 0)
{
int end = events[index][1];
int nextIndex = -1, low = index+1, high = n-1, mid;
while (low <= high)
{
mid = low+(high-low)/2;
if (events[mid][0] > end)
{
nextIndex = mid;
high = mid-1;
}
else low = mid+1;
}
result = Math.max(result,events[index][2]+count(events, k-1, nextIndex));
}
return values[index][k] = result;
}
}