最大重叠区间个数--java实现

主要思路来源于下面这个博客:

https://blog.csdn.net/qq_42060170/article/details/104241823

在细节上,做了一些修改:

(1)定义了新的Comparator接口,使之能正确针对左闭右开区间进行排序

(2)原文中的Point类对应于本文的Interval类,删除类原文中的Interval类

(3)增加了控制台的输入和输出

(4)原文中count初始值为1,本文中修改为0

(5)原文中Collections.sort()修改为Arrays.sort()

代码实现:


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


/**
 * 输入:
 *      第一行: 一个整数n, 代表之后要输入多少个区间
 *      之后, 每行有两个数, 例如: 3 6  其代表区间[3, 6)
 * 输入实例:
 *      4
 *      1 9
 *      2 3
 *      3 5
 *      3 6
 *
 * 输出:
 *      3
 *      该输出代表重叠区间个数的最大值
 *      上述输入所有可能的重叠为:
 *          1 9, 2 3
 *          1 9, 3 5
 *          1 9, 3 6
 *          3 5, 3 6
 *          1 9, 3 5, 3 6
 *      所以, 重叠区间个数的最大值为3, 即1 9, 3 5, 3 6这三个区间的重合
 */
public class OverlapIntervalCount {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if (n == 0){
            System.out.println(0);
        }
        Interval[] intervals = new Interval[n*2];
        int i = 0;
        while (i<n) {
            int start = in.nextInt();
            int end = in.nextInt();
            intervals[i*2] = new Interval(start, 0);
            intervals[i*2+1] = new Interval(end, 1);
            i++;
        }
        Arrays.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                if (o1.val < o2.val){
                    return -1;
                } else if (o1.val > o2.val){
                    return 1;
                } else {
                    if (o1.type == o2.type){
                        return 0;
                    } else {
                        // 由于类似[3, 5), [5, 6)这种不算是重合区间, 所以在排序时要保证在两个val相同时, type=1要在type=0前
                        if (o1.type > o2.type){
                            return -1;
                        } else {
                            return 1;
                        }
                    }
                }
            }
        });
        // 以下代码用于查看排序好的intervals数组中的所有元素
        for (int d=0;d<2*n;d++){
            System.out.println("type: "+intervals[d].type+", val: "+intervals[d].val);
        }
        // 核心代码
        int max = 1;
        int count = 0;
        for (int j=0;j<2*n;j++){
            if (intervals[j].type == 0){
                count++;
                max = Math.max(max, count);
            } else {
                // 说明某个区间已经闭合, 所以总计数应该减少1
                count--;
            }
        }
        System.out.println(max);
    }

}

class Interval{
    public int type;
    public int val;
    public Interval(int val, int type){
        this.type = type;
        this.val = val;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值