?LeetCode刷題實(shí)戰(zhàn)435:無重疊區(qū)間
?Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
示例
示例 1:
輸入: [ [1,2], [2,3], [3,4], [1,3]?]
輸出: 1
解釋: 移除 [1,3]?后,剩下的區(qū)間沒有重疊。
示例 2:
輸入: [ [1,2], [1,2], [1,2]?]
輸出: 2
解釋: 你需要移除兩個 [1,2]?來使剩下的區(qū)間沒有重疊。
示例 3:
輸入: [ [1,2], [2,3]?]
輸出: 0
解釋: 你不需要移除任何區(qū)間,因?yàn)樗鼈円呀?jīng)是無重疊的了。
解題
class?Solution?{
public:
????int?eraseOverlapIntervals(vector<vector<int>>& intervals)?{
????????if?(intervals.empty()) {
????????????return?0;
????????}
????????
????????sort(intervals.begin(), intervals.end(), [](const?auto& u, const?auto& v) {
????????????return?u[1] < v[1];
????????});
????????int?n = intervals.size();
????????int?right = intervals[0][1];
????????int?ans = 1;
????????for?(int?i = 1; i < n; ++i) {
????????????if?(intervals[i][0] >= right) {
????????????????++ans;
????????????????right = intervals[i][1];
????????????}
????????}
????????return?n - ans;
????}
};
LeetCode1-420題匯總,希望對你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)421:數(shù)組中兩個數(shù)的最大異或值
LeetCode刷題實(shí)戰(zhàn)422:有效的單詞方塊
LeetCode刷題實(shí)戰(zhàn)423:從英文中重建數(shù)字
LeetCode刷題實(shí)戰(zhàn)424:替換后的最長重復(fù)字符
LeetCode刷題實(shí)戰(zhàn)425:單詞方塊
LeetCode刷題實(shí)戰(zhàn)426:將二叉搜索樹轉(zhuǎn)化為排序的雙向鏈表
LeetCode刷題實(shí)戰(zhàn)427:建立四叉樹
LeetCode刷題實(shí)戰(zhàn)428:序列化和反序列化 N 叉樹
LeetCode刷題實(shí)戰(zhàn)429:N 叉樹的層序遍歷
LeetCode刷題實(shí)戰(zhàn)430:扁平化多級雙向鏈表
LeetCode刷題實(shí)戰(zhàn)431:將 N 叉樹編碼為二叉樹
LeetCode刷題實(shí)戰(zhàn)432:全 O(1) 的數(shù)據(jù)結(jié)構(gòu)
LeetCode刷題實(shí)戰(zhàn)433:最小基因變化
LeetCode刷題實(shí)戰(zhàn)434:字符串中的單詞數(shù)
