1. 程式人生 > >435. Non-overlapping Intervals

435. Non-overlapping Intervals

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note:

  1. You may assume the interval's end point is always bigger than its start point.
  2. Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.

Example 1:

Input: [ [1,2], [2,3], [3,4], [1,3] ]

Output: 1

Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.

Example 2:

Input: [ [1,2], [1,2], [1,2] ]

Output: 2

Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.

Example 3:


Input: [ [1,2], [2,3] ]

Output: 0

Explanation: You don't need to remove any of the intervals since they're already non-overlapping.

思路:貪心演算法,以每個元素的end為關鍵元素排序,end相等的情況下,按start升序排序。這裡的實際型別和這個問題很像,

只有一個場地,要安排儘量多的活動。貪心規則:儘量安排結束時間早的活動。如果後面的活動與已經安排好的相容,則加入集合。

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
    
public:
    int eraseOverlapIntervals(vector<Interval>& intervals) {
       int n=intervals.size();
       if(!n) return 0;
       sort(intervals.begin(),intervals.end(),[](Interval a,Interval b){
           return a.end<b.end||(a.start>b.start&&a.end==b.end);
       });
       int cnt=1, s=intervals[0].start,e=intervals[0].end;
       for(int i=1;i<n;i++)
       {
           //if(it.start==s&&it.end>e) continue;
           Interval it=intervals[i];
           if(it.start>=e) {
               cnt++;
              // s=it.start;
               e=it.end;
           }
       }
       return n-cnt;
    }
};


相關推薦

LeetCode 452. Minimum Number of Arrows to Burst Balloons & 435. Non-overlapping Intervals

題解 兩題一起寫,都是pair型陣列,都考慮交疊問題,都是貪心。 這類題的核心都在於預先sort一下,以second升序排(若相等再以first升序)。 這樣的好處是,我們再順序遍歷的時候可以很方便地發現交疊情況: 比如用一個 p 變數記載之前的second,新位置比較一下first

leetcode:(435) Non-overlapping Intervals(java)

題目:      Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals

python leetcode 435. Non-overlapping Intervals

類似於時刻安排表 最後的列表中每個end應該儘量要小 44ms 100% class Solution: def eraseOverlapIntervals(self, intervals): """ :type intervals: List[I

***Leetcode 435. Non-overlapping Intervals

很不錯的題 跟課程規劃那個題有點相似 bool cmp( const Interval& a , const Interval& b ) { if (a.start != b.start) return a.start < b.start;

LeetCode 452. Minimum Number of Arrows to Burst Balloons & 435. Non-overlapping Intervals

題解 兩題一起寫,都是pair型陣列,都考慮交疊問題,都是貪心。 這類題的核心都在於預先sort一下,以second升序排(若相等再以first升序)。 這樣的好處是,我們再順序遍歷的時候可以很方便地發

LeetCode練習題435. Non-overlapping Intervals

題目 Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlap

貪心專題1 - leetcode455. Assign Cookies/435. Non-overlapping Intervals

455. Assign Cookies 題目描述 假設你是一位很棒的家長,想給孩子們一些餅乾(每個孩子最多隻能給一塊餅乾)。 每個孩子 i 都有一個胃口值 gi (為正數),表示滿足孩子胃口的餅乾最小尺寸;每塊餅乾 j 的尺寸為 sj 。 若 sj >= gi ,將

435. Non-overlapping Intervals

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note: You may

Non-overlapping Intervals

結束的早不容易跟之後的overlap class Solution { public: int eraseOverlapIntervals(vector<Interval>& intervals) { if(intervals.e

[LeetCode] Non-overlapping Intervals 非重疊區間

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note: You

689. Maximum Sum of 3 Non-Overlapping Subarrays

then sub lis .com this phi multiple dice start Max Sum of Subarray with size k, 相當於Easy難度,我說用一個sum array存sum,然後做減法就行。中國小哥說讓優化空間,於是說可以只用兩個

[LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三個菲重疊子數組的最大和

turn array pin could plan swe ogr ppi etc In a given array nums of positive integers, find three non-overlapping subarrays with maximu

689. Maximum Sum of 3 Non-Overlapping Subarrays三個不重合數組的求和最大值

題目 分鐘 為什麽 lex OS star nat fin 空間分析 [抄題]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum su

[LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三個非重疊子數組的最大和

lee i+1 multi ssi push_back 數字 imu ant cnblogs In a given array nums of positive integers, find three non-overlapping subarrays with maxi

動態規劃——Maximum Sum of 3 Non-Overlapping Subarrays

使用 length 第一個 subarray n-2 [] 範圍 通過 兩個 這個題對我來說真的是相當難的題目了,嚴格來講可能不算是個動態規劃的題目,但這個題目對類似的劃分多個非重疊連續子區間的問題提供了一個很好解決方案 這個題目需要找三個非重疊的連續子區間,通過維護兩個

【LeetCode】497. Random Point in Non-overlapping Rectangles 解題報告(Python)

題目描述: Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks a

[LeetCode] Random Point in Non-overlapping Rectangles 非重疊矩形中的隨機點

Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly and uniformily picks an integer point in the space co

[LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三個非重疊子陣列的最大和

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maxim

[Swift]LeetCode497. 非重疊矩形中的隨機點 | Random Point in Non-overlapping Rectangles

ray 對齊 select ppi 構造 示例 runtime count etc Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which rand

Leetcode-1031 Maximum Sum of Two Non-Overlapping Subarrays(兩個非重疊子數組的最大和)

ppi array color ret ping define solution app fine 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4