Day 1: Lintcode【区间】【简单】【合并区间*2】

本文深入探讨了区间合并算法,包括如何合并重叠区间以及合并两个已排序的区间列表。通过具体示例,如[(1,3)]和[(1,3),(2,6),(8,10),(15,18)]的合并过程,展示了算法的实现细节和挑战,如达到O(nlogn)的时间复杂度和O(1)的额外空间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.
描述

Given a collection of intervals, merge all overlapping intervals.
样例
Example 1:
Input: [(1,3)]
Output: [(1,3)]

Example 2:
Input: [(1,3),(2,6),(8,10),(15,18)]
Output: [(1,6),(8,10),(15,18)]

挑战
O(n log n) time and O(1) extra space.
在这里插入图片描述
在这里插入图片描述

	/**
	 * Definition of Interval:
	 * classs Interval {
	 *     int start, end;
	 *     Interval(int start, int end) {
	 *         this->start = start;
	 *         this->end = end;
	 *     }
	 * }
	 */
	
	class Solution {
	public:
	    static bool cmp(const Interval &a, const Interval &b){
	        return a.start<b.start;
	    }
	    /**
	     * @param intervals: interval list.
	     * @return: A new interval list.
	     */
	    vector<Interval> merge(vector<Interval> &intervals) {
	        // write your code here
	        int n = intervals.size();
	        vector <Interval> result;
	        if(n<=1) return intervals;
	        
	        sort(intervals.begin(),intervals.end(),cmp);
	        int left = intervals[0].start;
	        int right = intervals[0].end;
	
	        for(int i=0; i<n; ++i){
	            if(intervals[i].start<=right){
	                right=max(right, intervals[i].end);
	            }
	            else{
	                result.push_back(Interval(left, right));
	                left = intervals[i].start;
	                right = intervals[i].end;
	            }
	        }
	        result.push_back(Interval(left,right));
	        return result;
	    }
	};

(思路借鉴brucehb大佬的~)
在这里插入图片描述
二.
描述
Merge two sorted (ascending) lists of interval and return it as a new sorted list. The new sorted list should be made by splicing together the intervals of the two lists and sorted in ascending order.

  • The intervals in the given list do not overlap.
  • The intervals in different lists may overlap.

样例
Example1

Input: list1 = [(1,2),(3,4)] and list2 = [(2,3),(5,6)]
Output: [(1,4),(5,6)]
Explanation:
(1,2),(2,3),(3,4) --> (1,4)
(5,6) --> (5,6)
Example2

Input: list1 = [(1,2),(3,4)] and list2 = [(4,5),(6,7)]
Output: [(1,2),(3,5),(6,7)]
Explanation:
(1,2) --> (1,2)
(3,4),(4,5) --> (3,5)
(6,7) --> (6,7)

/**
 * Definition of Interval:
 * classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param list1: one of the given list
     * @param list2: another list
     * @return: the new sorted list of interval
     */
    static bool cmp(const Interval &a, const Interval &b){
        return a.start<b.start;
    }
    vector<Interval> mergeTwoInterval(vector<Interval> &list1, vector<Interval> &list2) {
        // write your code here
        vector <Interval> merge, result;
        int n1 = list1.size();
        int n2 = list2.size();
        for(int i=0; i<n1; ++i){
            merge.push_back(list1[i]);
        }
        for(int i=0; i<n2; ++i){
            merge.push_back(list2[i]);
        }
        sort(merge.begin(),merge.end(),cmp);
        int n3= n1+n2;
        if(n3<=1) return merge;
        int left = merge[0].start, right = merge[0].end;
        for(int i=0; i<n3; ++i){
            if(merge[i].start<=right){
                right = max(right, merge[i].end);
            }
            else{
                result.push_back(Interval(left, right));   
                left = merge[i].start; right = merge[i].end;
            }
        }
        result.push_back(Interval(left, right));
        return result;
    }
};

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值