LeetCode 3414: Maximum Score of Non-overlapping Intervals Solution

Master LeetCode problem 3414 (Maximum Score of Non-overlapping Intervals), 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.

3414. Maximum Score of Non-overlapping Intervals

Problem Explanation

Explanation:

To solve this problem, we can use dynamic programming to find the maximum score of non-overlapping intervals. We will sort the intervals based on their ending points in ascending order. Then, we will iterate through the intervals and keep track of the maximum score we can achieve up to that interval using a dynamic programming array.

  1. Sort the intervals based on their ending points in ascending order.
  2. Initialize a dynamic programming array dp to store the maximum score we can achieve up to each interval.
  3. Iterate through the sorted intervals and for each interval, calculate the maximum score we can achieve up to that interval by considering either including the current interval or skipping it.
  4. Keep track of the previous interval that contributes to the maximum score and update the dp array accordingly.
  5. Finally, backtrack to find the indices of the chosen intervals that contribute to the maximum score.

Time Complexity:

The time complexity of this approach is O(n log n) for sorting the intervals and O(n) for iterating through the intervals, where n is the number of intervals.

Space Complexity:

The space complexity is O(n) for the dynamic programming array and O(n) for storing the backtracking information.

: :

Solution Code

import java.util.*;

class Solution {
    public int[] maxNonOverlapping(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
        int n = intervals.length;
        int[] dp = new int[n];
        int[] prev = new int[n];
        
        Arrays.fill(prev, -1);
        
        for (int i = 0; i < n; i++) {
            dp[i] = intervals[i][2];
            for (int j = i - 1; j >= 0; j--) {
                if (intervals[j][1] <= intervals[i][0]) {
                    if (dp[j] + intervals[i][2] > dp[i]) {
                        dp[i] = dp[j] + intervals[i][2];
                        prev[i] = j;
                    }
                    break;
                }
            }
        }
        
        List<Integer> resList = new ArrayList<>();
        int idx = n - 1;
        while (idx >= 0) {
            if (prev[idx] != -1) {
                resList.add(idx);
                idx = prev[idx];
            } else {
                idx--;
            }
        }
        
        int[] res = new int[resList.size()];
        for (int i = 0; i < resList.size(); i++) {
            res[i] = resList.get(resList.size() - i - 1);
        }
        
        return res;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3414 (Maximum Score of Non-overlapping Intervals)?

This page provides optimized solutions for LeetCode problem 3414 (Maximum Score of Non-overlapping Intervals) 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 3414 (Maximum Score of Non-overlapping Intervals)?

The time complexity for LeetCode 3414 (Maximum Score of Non-overlapping Intervals) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3414 on DevExCode?

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

Back to LeetCode Solutions