首页 > 其他分享 >LeetCode 435. Non-overlapping Intervals

LeetCode 435. Non-overlapping Intervals

时间:2022-11-08 19:47:31浏览次数:41  
标签:Non vector return Intervals int intervals 435 LeetCode

贪心

按照有边界排序,只有先选择了右边边界小的,才可以放下更多的区间

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if(intervals.empty())
            return 0;
        int n = intervals.size();
        sort(intervals.begin(),intervals.end(),[](vector<int>&a,vector<int>&b){
            return a[1]<b[1];
        });
        int total = 0,prev = intervals[0][1];
        for(int i=1;i<n;i++){
            if(intervals[i][0]<prev)
                total++;
            else
                prev = intervals[i][1];
        }
        return total;
    }
};

标签:Non,vector,return,Intervals,int,intervals,435,LeetCode
From: https://www.cnblogs.com/poteitoutou/p/16870907.html

相关文章

  • leetcode-1260-easy
    Shift2DGridGivena2Dgridofsizemxnandanintegerk.Youneedtoshiftthegridktimes.Inoneshiftoperation:Elementatgrid[i][j]movestogrid......
  • leetcode-561-easy
    ArrayPartitionGivenanintegerarraynumsof2nintegers,grouptheseintegersintonpairs(a1,b1),(a2,b2),...,(an,bn)suchthatthesumofmin(ai,bi......
  • leetcode-1854-easy
    MaximumPopulationYearYouaregivena2Dintegerarraylogswhereeachlogs[i]=[birthi,deathi]indicatesthebirthanddeathyearsoftheithperson.The......
  • leetcode-1684-easy
    CounttheNumberofConsistentStringsYouaregivenastringallowedconsistingofdistinctcharactersandanarrayofstringswords.Astringisconsistenti......
  • LeetCode 135. Candy
    贪心算法贪心策略:在每次遍历中,仅考虑并更新相邻一侧的大小关系classSolution{public:intcandy(vector<int>&ratings){intsize=ratings.size();......
  • LeetCode 455. Assign Cookies
    贪心classSolution{public:intfindContentChildren(vector<int>&g,vector<int>&s){sort(g.begin(),g.end());sort(s.begin(),s.end());......
  • Leetcode练题系列(六): 字符串相关的算法
    LeetCode  ​​英文官网(推荐)​​  ​​中文官网​​  从2016年大二左右开始就接触算法,起初也简单练习过,但现在工作一段时间后,随着代码水平的提高(​​自我感觉​​)......
  • 【Leetcode】 剑指offer:链表(简单)--Day02
    剑指Offer06.从尾到头打印链表可借助栈。或先遍历列表得到元素数,开辟数组空间倒序填入。剑指Offer24.反转链表可借助栈:classSolution{publicListNodere......
  • Leetcode第1684题:统计一致字符串的数目(Count the number of consistent strings)
    解题思路采用位运算的思路不太好理解。但思想就是根据allowed建立一个\(mask\),遍历words中的每个元素的每个字符c,查看\(mask\)的值是否为真。如果存在就返回结果加一。......
  • leetcode 541. 反转字符串 II
    题目给定一个字符串s和一个整数k,从字符串开头算起,每计数至2k个字符,就反转这2k字符中的前k个字符。如果剩余字符少于k个,则将剩余字符全部反转。如果剩余字符......