LC.P1751[最多可以参加的会议数目II]

方法一:动态规划+二分查找

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
class Solution {
public int maxValue(int[][] events, int k) {
Arrays.sort(events, (a, b) -> a[1] - b[1]);
int n = events.length;
int[][] f = new int[n + 1][k + 1];
for (int i = 0; i < n; ++i) {
int p = search(events, i, events[i][0]);
for (int j = 1; j <= k; ++j) {
f[i + 1][j] = Math.max(f[i][j], f[p + 1][j - 1] + events[i][2]);
}
}
return f[n][k];
}

// 返回 endDay < upper 的最大下标
private int search(int[][] events, int right, int upper) {
int left = -1;
while (left + 1 < right) {
int mid = left + right >> 1;
if (events[mid][1] < upper) left = mid;
else right = mid;
}
return left;
}
}
  • 时间复杂度:$O(nk + nlogn)$
  • 空间复杂度:$O(nk)$