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题解-55. Jump Game && 56. Merge Intervals

55, 题目:Given an array of non-negative integers, you are initially positioned at the first index of t...

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

题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int sta...

LeetCode435. Non-overlapping Intervals题解

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

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

Gas Station 1. 问题描述 有N个加油站围成一个圈,第i个加油站的油量是gas[i],一辆车有一个无限量的油箱,每次从第i个加油站开到第i+1个加油站需要消耗cost[i]的油量。车一开始...

leetcode——Merge Intervals

题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1...

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
  • 268

LeetCode题解——Palindrome Pairs

题目: Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list...

LeetCode题解——Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. Fo...

LeetCode题解——Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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