LeetCode-1751. Maximum Number of Events That Can Be Attended II (Maximum Number of Events That Can Be Attended II) [Difficulty]-Analysis and Code (Java)

LeetCode——1751. Maximum Number of Events That Can Be Attended II[Maximum Number of Events That Can Be Attended II][Difficulty]——Analysis and Code [Java]

1. Topic

Give you an events array, where events[i] = [startDayi, endDayi, valuei], which means that the i-th meeting starts on the startDayi day and ends on the endDayi day. If you participate in this meeting, you can get the value valuei. At the same time, give you an integer k to indicate the maximum number of meetings you can participate in.
You can only participate in one meeting at a time. If you choose to attend a meeting, then you must attend the meeting in its entirety. The end date of the meeting is included in the meeting, which means that you cannot participate in two meetings with the same start date and the same end date at the same time.
Please return the maximum meeting value you can get.

Example 1:

输入:events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
输出:7
解释:选择绿色的活动会议 0 和 1,得到总价值和为 4 + 3 = 7 。

Example 2:

输入:events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
输出:10
解释:参加会议 2 ,得到价值和为 10 。
你没法再参加别的会议了,因为跟会议 2 有重叠。你 不 需要参加满 k 个会议。

Example 3:

输入:events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
输出:9
解释:尽管会议互不重叠,你只能参加 3 个会议,所以选择价值最大的 3 个会议。

prompt:

  • 1 <= k <= events.length
  • 1 <= k * events.length <= 10^6
  • 1 <= startDayi <= endDayi <= 10 ^ 9
  • 1 <= valuei <= 10^6

Source: LeetCode
Link: https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, analysis and code

1. Dynamic programming

(1) Thinking

First, the conferences are sorted by end time, and the dynamic programming array dp[i][j] is designed to represent the maximum value obtained by participating in j conferences in the first i conferences.
Then dp[i][j] = max(dp[i-1][j], dp[the last non-conflicting meeting][j-1] + events[the i-th meeting after sorting]).
Furthermore, because the conferences have been sorted by end time, the dichotomy can be used to find the last non-conflicting conference.

(2) Code

class Solution {
    
    
    public int maxValue(int[][] events, int k) {
    
    
        int n = events.length;
        Arrays.sort(events, new Comparator<int[]>() {
    
    //按照会议结束时间排序
           public int compare(int[] e1, int[] e2) {
    
    
                return e1[1] - e2[1];
           } 
        });

        int[][] dp = new int[n + 1][k + 1];//前i个会议中参加j个会议的最大价值
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= k; j++)
                dp[i][j] = 0;
        for (int i = 0; i < n; i++) {
    
    
            //二分查找得到上一个不冲突的会议,对应结果为 l - 1
            int start = events[i][0], l = 0, r = i;//左闭右开
            while (l < r) {
    
    
                int m = (l + r) >> 1;
                if (events[m][1] < start)
                    l = m + 1;
                else
                    r = m;
            }
            for (int j = 1; j <= k; j++) {
    
    //events[i]对应dp[i + 1]
                dp[i + 1][j] = Math.max(dp[i][j], dp[l][j - 1] + events[i][2]);
            }
        }

        int ans = 0;
        for (int i = 1; i <= n; i++)
            ans = Math.max(ans, dp[i][k]);
        return ans;
    }
}

(3) Results

Execution time: 46 ms, beating 82.38% of users
in all Java submissions ; memory consumption: 80.7 MB, beating 52.05% of users in all Java submissions.

Three, other

Nothing.

Guess you like

Origin blog.csdn.net/zml66666/article/details/114005137