LeetCode 1751: Maximum Number of Events That Can Be Attended II Solution

Master LeetCode problem 1751 (Maximum Number of Events That Can Be Attended II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1751. Maximum Number of Events That Can Be Attended II

Problem Explanation

Explanation

To solve this problem, we can use dynamic programming. We will first sort the events based on their end days. Then, we will iterate through each event and for each event, we will consider two cases: either attend the current event or skip it. We will maintain a 2D dp array where dp[i][j] represents the maximum sum of values that can be obtained by attending j events from the first i events.

At each event, we will update the dp array based on the maximum value we can get by attending the current event or skipping it. Finally, the maximum value we can get will be the maximum value in the last row of the dp array.

The time complexity of this solution is O(nk) where n is the number of events and k is the maximum number of events we can attend. The space complexity is also O(nk) for storing the dp array.

Solution Code

class Solution {
    public int maxValue(int[][] events, int k) {
        Arrays.sort(events, (a, b) -> Integer.compare(a[1], b[1]));
        
        int n = events.length;
        int[][] dp = new int[n + 1][k + 1];
        
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= k; j++) {
                int start = events[i - 1][0];
                int end = events[i - 1][1];
                int value = events[i - 1][2];
                
                int prevEventIndex = findPrevEvent(events, i);
                
                dp[i][j] = Math.max(dp[i - 1][j], dp[prevEventIndex][j - 1] + value);
            }
        }
        
        return dp[n][k];
    }
    
    private int findPrevEvent(int[][] events, int currentIndex) {
        int low = 0, high = currentIndex - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (events[mid][1] < events[currentIndex - 1][0]) {
                if (events[mid + 1][1] < events[currentIndex - 1][0]) {
                    low = mid + 1;
                } else {
                    return mid + 1;
                }
            } else {
                high = mid - 1;
            }
        }
        return 0;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1751 (Maximum Number of Events That Can Be Attended II)?

This page provides optimized solutions for LeetCode problem 1751 (Maximum Number of Events That Can Be Attended II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1751 (Maximum Number of Events That Can Be Attended II)?

The time complexity for LeetCode 1751 (Maximum Number of Events That Can Be Attended II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1751 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1751 in Java, C++, or Python.

Back to LeetCode Solutions