LeetCode题解——Intervals相关题总结

原创 2016年06月02日 09:34:22

1.Merge Intervals

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

思路:用贪心法,先按照每个区间的start position进行排序,排序之后进行合并。代码如下:

/**
 * 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:
    vector<Interval> merge(vector<Interval>& intervals) {
        int n = intervals.size();
        if(n<=1) return intervals;
        vector<Interval> res;
        
        sort(intervals.begin(), intervals.end(), comp);
        res.push_back(intervals[0]);
        for(int i=1; i<n; i++){
            auto pre = res.back();
            if(pre.end>intervals[i].end) continue;
            else if(pre.end<intervals[i].start) {
                res.push_back(intervals[i]);
            }
            else{//merge
                intervals[i].start = pre.start;
                res.pop_back();
                res.push_back(intervals[i]);
            }
        }
        return res;
    }
    
    static bool comp(const Interval& a, const Interval& b){
        return a.start < b.start;
    }
};

2. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

思路:找到该区间应该插入的位置it,it->start>=start,然后分情况进行处理

class Solution {
public:
    vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
        auto compare = [] (const Interval &intv1, const Interval &intv2)
                          { return intv1.end < intv2.start; };
        auto range = equal_range(intervals.begin(), intervals.end(), newInterval, compare);
        auto itr1 = range.first, itr2 = range.second;
        if (itr1 == itr2) {
            intervals.insert(itr1, newInterval);
        } else {
            itr2--;
            itr2->start = min(newInterval.start, itr1->start);
            itr2->end = max(newInterval.end, itr2->end);
            intervals.erase(itr1, itr2);
        }
        return intervals;
    }
};

3.Data Stream as Disjoint Intervals

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be:

[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]

struct Interval {
     int start;
     int end;
     Interval() : start(0), end(0) {}
     Interval(int s, int e) : start(s), end(e) {}
 };

 
class SummaryRanges {
public:
    void addNum(int val) {
	auto Cmp = [](Interval a, Interval b) { return a.start < b.start; };
        auto it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
        int start = val, end = val;
        if(it != vec.begin() && (it-1)->end+1 >= val) it--;
        while(it != vec.end() && val+1 >= it->start && val-1 <= it->end)
        {
            start = min(start, it->start);
            end = max(end, it->end);
            it = vec.erase(it);
        }
        vec.insert(it,Interval(start, end));
    }

    vector<Interval> getIntervals() {
        return vec;
    }
private:
    vector<Interval> vec;
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

Merge Intervals  Given a collection of intervals, merge all overlapping intervals. For example...
  • xygy8860
  • xygy8860
  • 2015年07月15日 14:06
  • 1351

LeetCode总结,动态规划问题小结

一,参考一般书籍中的“动态规划”讲解 1、基本概念 动态规划(Dynamic Programming)对于子问题重叠的情况特别有效,因为它将子问题的解保存在表格中,当需要某个子问题的解时,直接取值即...
  • EbowTang
  • EbowTang
  • 2016年03月04日 23:50
  • 5444

LeetCode总结 -- 一维动态规划篇

这篇文章的主题是动态规划, 主要介绍LeetCode中一维动态规划的题目, 列表如下: Climbing StairsDecode WaysUnique Binary Search TreesMaxi...
  • linhuanmars
  • linhuanmars
  • 2014年08月10日 11:16
  • 21769

leetcode题解-55. Jump Game && 56. Merge Intervals

55, 题目:Given an array of non-negative integers, you are initially positioned at the first index of t...
  • liuchonge
  • liuchonge
  • 2017年05月09日 20:15
  • 630

【LeetCode】Merge Intervals 题解 利用Comparator进行排序

题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int sta...
  • u011955072
  • u011955072
  • 2015年03月15日 23:54
  • 1079

LeetCode435. Non-overlapping Intervals题解

Given a collection of intervals, find the minimum number of intervals you need to remove to make the...
  • lby0910
  • lby0910
  • 2017年04月12日 18:44
  • 294

Leetcode——134. Gas Station && 435. Non-overlapping Intervals

Gas Station 1. 问题描述 有N个加油站围成一个圈,第i个加油站的油量是gas[i],一辆车有一个无限量的油箱,每次从第i个加油站开到第i+1个加油站需要消耗cost[i]的油量。车一开始...
  • parishong
  • parishong
  • 2017年06月19日 09:36
  • 230

leetcode——Merge Intervals

题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1...
  • langduhualangdu
  • langduhualangdu
  • 2014年12月08日 15:28
  • 429

LeetCode刷题题解141道C++版

  • 2017年11月01日 11:56
  • 12KB
  • 下载

LeetCode OJ算法题(五十五):Merge Intervals

题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1...
  • op_yu
  • op_yu
  • 2014年08月08日 21:12
  • 299
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode题解——Intervals相关题总结
举报原因:
原因补充:

(最多只允许输入30个字)