LC_2_1贪心算法 区间调度问题

给你很多形如 [start, end] 的闭区间,请你设计一个算法,算出这些区间中最多有几个互不相交的区间

Non-overlapping Intervals

https://leetcode.com/problems/non-overlapping-intervals/description/

Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

区间调度问题是让你计算若干区间中最多有几个互不相交的区间,这道题是区间调度问题的一个简单变体。

区间调度问题思路可以分为以下三步:

1、从区间集合 intvs 中选择一个区间 x,这个 x 是在当前所有区间中结束最早的(end 最小)。

2、把所有与 x 区间相交的区间从区间集合 intvs 中删除。

3、重复步骤 1 和 2,直到 intvs 为空为止。之前选出的那些 x 就是最大不相交子集。

img
 public int eraseOverlapIntervals(int[][] intervals) {
        return intervals.length - intervalSchedule(intervals);
    }

    //区间调度问题 - 计算出不相交的区间个数
    private static int intervalSchedule(int[][] intervals) {
        //根据end进行升序排序
        Arrays.sort(intervals,new Comparator<int[]>() {
            public int compare(int[] a, int[] b) {
                return a[1] - b[1];
            }
        });

        int x_end = intervals[0][1];// 拿到最小end区间的最小end
        int count = 1;//计算的是不相交的区间个数,即使全相交,那也有一个不相交,至少就一个
        for(int i = 1; i < intervals.length; i++) {
            if(x_end <= intervals[i][0]) {
                count++;
                x_end = intervals[i][1];
            }
        }
        return count;
    }

Minimum Number of Arrows to Burst Balloons

https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

Given the array points, return the minimum number of arrows that must be shot to burst all balloons.

public int findMinArrowShots(int[][] points) {
        return intervalSchedule(points);
    }


    //贪心算法 - 区间调度问题 - 计算不相交区间的个数
    private static int intervalSchedule(int[][] intervals) {
        //根据end进行升序排序
        Arrays.sort(intervals, new Comparator<int[]>() {
            public int compare(int[] a, int[] b) {
                if(a[1] < b[1]) {
                    return -1;
                }else if(a[1] == b[1]) {
                    return 0;
                }else {
                    return 1;
                }
            }
        });

        int count = 1; //被x_end覆盖到的区间有几个
        int x_end = intervals[0][1];
        for(int i = 1; i < intervals.length; i++) {
            if(x_end < intervals[i][0]) {
                count++;
                x_end = intervals[i][1];
            }
        }
        return count;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HackerTerry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值