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
The text was updated successfully, but these errors were encountered:
Woodyiiiiiii
changed the title
Leetcode 2054. Two Best Non-Overlapping Events
Leetcode 2054. Two Best Non-Overlapping Events / 1235. Maximum Profit in Job Scheduling
Aug 23, 2023
这种LIS的类型题目如下:
还有:
这类题型的问题模版一般是:
与LIS不同的是,不能直接用贪心去维护一个绝对单调递增的数列,因为加入了新的一个变量(利润),所以要遍历所有可能的结果,选择最大的那个分枝。
解题思路是自顶向下的DP/记忆化搜索。先排序,再遍历数组,对于该当前元素分为两种情况,一种是选择该元素,另一种是不选择。递进公式是
dp[i][j] = Math.max(dfs(events, i + 1, j, k, dp), dfs(events, nextIdx, j + 1, k, dp) + events[i][2])
。选择该元素的情况下,就要通过二分法找到该元素下一个不相交冲突的元素;否则遍历到下一个元素。DFS本来是要O(klgn2^n),但因为有记忆化搜索,时间复杂度为O(knlgn)。
以1751. Maximum Number of Events That Can Be Attended II为例子:(可以从限制条件n * k的范围推算可以用记忆化)
23/07/15我用DP复写了一遍,不同的是排序时要先以结束时间排序:
如果是k不限制的情况下1235. Maximum Profit in Job Scheduling,则不用二维数组:
2054. Two Best Non-Overlapping Events这道题因为k确定是2,可以用堆来做。
这有道类似的题目,可以锻炼下逻辑思维,有空可以做一下:1353. Maximum Number of Events That Can Be Attended
The text was updated successfully, but these errors were encountered: