LeetCode 题解工作台
不重叠区间的最大得分
给你一个二维整数数组 intervals ,其中 intervals[i] = [l i , r i , weight i ] 。区间 i 的起点为 l i ,终点为 r i ,权重为 weight i 。你最多可以选择 4 个互不重叠 的区间。所选择区间的 得分 定义为这些区间权重的总和。 返回一…
4
题型
0
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个二维整数数组 intervals,其中 intervals[i] = [li, ri, weighti]。区间 i 的起点为 li,终点为 ri,权重为 weighti。你最多可以选择 4 个互不重叠 的区间。所选择区间的 得分 定义为这些区间权重的总和。
返回一个至多包含 4 个下标且 字典序最小 的数组,表示从 intervals 中选中的互不重叠且得分最大的区间。
如果两个区间没有任何重叠点,则称二者 互不重叠 。特别地,如果两个区间共享左边界或右边界,也认为二者重叠。
示例 1:
输入: intervals = [[1,3,2],[4,5,2],[1,5,5],[6,9,3],[6,7,1],[8,9,1]]
输出: [2,3]
解释:
可以选择下标为 2 和 3 的区间,其权重分别为 5 和 3。
示例 2:
输入: intervals = [[5,8,1],[6,7,7],[4,7,3],[9,10,6],[7,8,2],[11,14,3],[3,5,5]]
输出: [1,3,5,6]
解释:
可以选择下标为 1、3、5 和 6 的区间,其权重分别为 7、6、3 和 5。
提示:
1 <= intervals.length <= 5 * 104intervals[i].length == 3intervals[i] = [li, ri, weighti]1 <= li <= ri <= 1091 <= weighti <= 109
解题思路
方法一
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Focus on dynamic programming state transitions and their efficiency.
- question_mark
Ensure understanding of sorting and binary search integration for this problem.
- question_mark
Evaluate the candidate's ability to handle large inputs within time limits.
常见陷阱
外企场景- error
Failing to handle the overlap condition correctly, especially when intervals share boundaries.
- error
Using an inefficient approach without leveraging binary search or sorting, leading to poor performance.
- error
Not accounting for lexicographical order when multiple sets of intervals yield the same score.
进阶变体
外企场景- arrow_right_alt
Allowing more than four intervals to be selected while maximizing the score.
- arrow_right_alt
Considering a variant where intervals have different constraints on the weight values or boundaries.
- arrow_right_alt
Introducing a constraint where the intervals must follow a specific order in their left and right boundaries.