代码随想录刷题第35天

代码随想录刷题第35天

无重叠区间


/*

* @lc app=leetcode.cn id=435 lang=cpp

*

* [435] 无重叠区间

*

* https://leetcode.cn/problems/non-overlapping-intervals/description/

*

* algorithms

* Medium (51.16%)

 * Likes:    890

* Dislikes: 0

 * Total Accepted:    198.8K

* Total Submissions: 388.6K

 * Testcase Example:  '[[1,2],[2,3],[3,4],[1,3]]'

*

* 给定一个区间的集合 intervals ,其中 intervals[i] = [starti, endi] 。返回

* 需要移除区间的最小数量,使剩余区间互不重叠 。

* 

* 

* 

* 示例 1:

* 

* 

* 输入: intervals = [[1,2],[2,3],[3,4],[1,3]]

* 输出: 1

* 解释: 移除 [1,3] 后,剩下的区间没有重叠。

* 

* 

* 示例 2:

* 

* 

* 输入: intervals = [ [1,2], [1,2], [1,2] ]

* 输出: 2

* 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。

* 

* 

* 示例 3:

* 

* 

* 输入: intervals = [ [1,2], [2,3] ]

* 输出: 0

* 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。

* 

* 

* 

* 

* 提示:

* 

* 

* 1 <= intervals.length <= 10^5

* intervals[i].length == 2

* -5 * 10^4 <= starti < endi <= 5 * 10^4

* 

* 

*/

 

// @lc code=start

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

 

 

class Solution {

public:

 

    static bool cmp(vector<int> &a,vector<int> &b){

        return a[0] < b[0];

    }

    int eraseOverlapIntervals(vector<vector<int>>& intervals) {

        if (intervals.size() == 1)

        {

            return 0;

        }

 

        int result = 0;

        sort(intervals.begin(),intervals.end(),cmp);

        for (int i = 1; i < intervals.size(); i++)

        {

            if (intervals[i][0]  >= intervals[i-1][1])

            {

 

            }else{

                result++;

                intervals[i][1] = min(intervals[i-1][1],intervals[i][1]);

            }

 

        }

        return result;

    }

};

// @lc code=end

 

 

划分字母区间


/*

* @lc app=leetcode.cn id=763 lang=cpp

*

* [763] 划分字母区间

*

* https://leetcode.cn/problems/partition-labels/description/

*

* algorithms

* Medium (76.88%)

 * Likes:    897

* Dislikes: 0

 * Total Accepted:    150.4K

* Total Submissions: 195.6K

 * Testcase Example:  '"ababcbacadefegdehijhklij"'

*

* 给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。

* 

* 注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 s 。

* 

* 返回一个表示每个字符串片段的长度的列表。

* 

* 

* 示例 1:

* 

* 

* 输入:s = "ababcbacadefegdehijhklij"

* 输出:[9,7,8]

* 解释:

* 划分结果为 "ababcbaca"、"defegde"、"hijhklij" 。

* 每个字母最多出现在一个片段中。

* 像 "ababcbacadefegde", "hijhklij" 这样的划分是错误的,因为划分的片段数较少。 

* 

* 示例 2:

* 

* 

* 输入:s = "eccbbbbdec"

* 输出:[10]

* 

* 

* 

* 

* 提示:

* 

* 

* 1 <= s.length <= 500

* s 仅由小写英文字母组成

* 

* 

*/

 

// @lc code=start

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

 

class Solution {

public:

    vector<int> partitionLabels(string s) {

        int rep [27] = {0};

        for (int i = 0; i < s.size(); i++)

        {

            rep[s[i] - 'a'] = i;

        }

        vector<int> result ;

        int left = 0;

        int right = 0;

        for (int i = 0; i < s.size(); i++)

        {

            right = max(right , rep[s[i] - 'a']);

            if (i == right)

            {

                result.push_back(right - left + 1);

                left = i + 1 ;

            }

 

        }

 

        return result;

 

    }

};

// @lc code=end

 

 

56. 合并区间


/*

* @lc app=leetcode.cn id=56 lang=cpp

*

* [56] 合并区间

*

* https://leetcode.cn/problems/merge-intervals/description/

*

* algorithms

* Medium (49.25%)

 * Likes:    1798

* Dislikes: 0

 * Total Accepted:    588.1K

* Total Submissions: 1.2M

 * Testcase Example:  '[[1,3],[2,6],[8,10],[15,18]]'

*

* 以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi]

* 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。

* 

* 

* 

* 示例 1:

* 

* 

* 输入:intervals = [[1,3],[2,6],[8,10],[15,18]]

* 输出:[[1,6],[8,10],[15,18]]

* 解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

* 

* 

* 示例 2:

* 

* 

* 输入:intervals = [[1,4],[4,5]]

* 输出:[[1,5]]

* 解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。

* 

* 

* 

* 提示:

* 

* 

* 1 <= intervals.length <= 10^4

* intervals[i].length == 2

* 0 <= starti <= endi <= 10^4

* 

* 

*/

 

// @lc code=start

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

class Solution {

public:

 

    static bool cmp(vector<int> &a,vector<int>&b){

        return a[0] < b[0];

    }

    vector<vector<int>> merge(vector<vector<int>>& intervals) {

        sort(intervals.begin(),intervals.end(),cmp);

        vector<int> path;

        vector<vector<int>> result;

        int i ;

        for ( i = 1; i < intervals.size(); i++)

        {

            if (intervals[i][0] <= intervals[i-1][1])

            {

                int end = max(intervals[i][1],intervals[i-1][1]);

                intervals[i] = {intervals[i-1][0],end};

            }else{

 

                result.push_back(intervals[i-1]);

            }

 

        }

        result.push_back(intervals[i-1]);

        return result;

    }

};

// @lc code=end

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值