在给定的一组间隔里插入一个新的间隔

问题:

给定多个不重合的间隔(interval),比如 [3, 8] [ 12, 17] [22, 29], 然后插入一个新的间隔, 比如 [ 5, 15], 求最后不重合的间隔。比如,起初的不重合间隔变成 [3, 17] [22, 29].

分析:

首先,我们要知道怎么确定两个间隔是重合的。比如[1, 5] [3, 7] 是重合的,[1, 5] [6, 7] 是不重合的。那么,观察后发现,如果两个间隔的起始值的最大值小于两个间隔的结束值的最小值,那么,这两个间隔一定重合。代码如下:

	/*
	 * s1: start point of interval 1
	 * e1: end point of interval 1
	 * s2: start point of interval 2
	 * e2: end point of interval 2
	 */
	public boolean overlap(int s1, int e1, int s2, int e2) {
		return Math.max(s1, s2) < Math.min(e1, e2);
	}
假定给定的间隔按照起始值已经排序了,就是说第一个间隔的起始值是所有间隔起始值中最小的,最后一个间隔的起始值是所有间隔起始值中最大的。当插入一个新的间隔时,我们只需要判断它是否和每一个间隔相交,如果不相交,保持不变,如果相交,则把新的间隔求出来,再继续和后面的间隔进行判断。代码如下:

public class Test {
	//check whether the two intervals intersect
	public boolean overlap(Interval i1, Interval i2) {
		return Math.max(i1.start, i2.start) < Math.min(i1.end, i2.end);
	}
	
	//add the new interval to the interval list and return a new interval list without overlapping
	public ArrayList<Interval> add(ArrayList<Interval> list, Interval interval) {
		ArrayList<Interval> newList = new ArrayList<Interval>();
		int i = 0;
		while (i < list.size()) {
			//no intersection
			if (overlap(list.get(i), interval) == false) {
				newList.add(list.get(i));
				i++;
			} else {
				//get the new interval
				while (i < list.size() && overlap(list.get(i), interval) == true) {
					int start = Math.min(list.get(i).start, interval.start);
					int end = Math.max(list.get(i).end, interval.end);
					interval = new Interval(start, end);
					i++;
				}
				newList.add(interval);
			}
		}
		return newList;
	}

	public static void main(String[] args) {
		ArrayList<Interval> list = new ArrayList<Interval>();
		
		list.add(new Interval(3, 8));
		list.add(new Interval(22, 29));
		list.add(new Interval(12, 17));
		list.add(new Interval(40, 47));
		Collections.sort(list);
		Interval interval = new Interval(40, 48);
		
		Test test = new Test();
		ArrayList<Interval> newList = test.add(list, interval);
		for(Interval inter : newList) {
			System.out.println("[" + inter.start + ", " + inter.end + "]");
		}
	}
}

class Interval implements Comparable<Interval>{
	int start;
	int end;
	
	Interval(int start, int end) {
		this.start = start;
		this.end = end;
	}

	public int compareTo(Interval o) {
		if (start > ((Interval)o).start || 
				(start == ((Interval)o).start && end > ((Interval)o).end)) {
				return 1;
		} else if (start == ((Interval)o).start && end == ((Interval)o).end) {
				return 0;
		}
		return -1;
	}
}

Another solution

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
        ArrayList<Interval> listAfterMerged = new ArrayList<Interval>();
        if (intervals == null || intervals.size() == 0) {
            listAfterMerged.add(newInterval);
            return listAfterMerged;
        }
        boolean isAdded = false;
        
        for (int i = 0; i < intervals.size(); i++) {
            Interval interv =  intervals.get(i);
            if (isAdded == false) {
                if (interv.start < newInterval.start) {
                    addInterval(listAfterMerged, interv);
                } else {
                    addInterval(listAfterMerged, newInterval);
                    isAdded = true;
                    addInterval(listAfterMerged, interv);
                }
            } else {
                addInterval(listAfterMerged, interv);
            }
        }
        if (isAdded == false) {
            addInterval(listAfterMerged, newInterval);
        }
        return listAfterMerged;
    }
    
    public void addInterval(ArrayList<Interval> listAfterMerged, Interval newInterval) {
        if (listAfterMerged.size() == 0) {
            listAfterMerged.add(newInterval);
        } 
        Interval interv =  listAfterMerged.get(listAfterMerged.size() - 1);
        if (interv.end >= newInterval.start) {
            interv.end = (int) Math.max(interv.end, newInterval.end);
        } else {
            listAfterMerged.add(newInterval);
        }
    }
    
    
    boolean isOverlap(Interval i1, Interval i2) {
        if (Math.max(i1.start, i2.start) <= Math.min(i1.end, i2.end)) return true;
        return false;
    }
}

转载请注明出处:http://blog.csdn.net/beiyeqingteng


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值