区间问题

区间问题

1. 缩

LeetCode:452. 用最少数量的箭引爆气球

class Solution {
    public int findMinArrowShots(int[][] points) {
    	int res = 0;
		List<Point> list = new ArrayList<>();
		for (int[] point : points) {
			list.add(new Point(point[0], point[1]));
		}
		Collections.sort(list, new Comparator<Point>() {
			@Override
			public int compare(Point o1, Point o2) {
				if (o1.left != o2.left){
					return Long.compare(o1.left, o2.left);
				}
				return Long.compare(o1.right, o2.right);
			}
		});

		for (int i = 0; i < list.size(); i++) {
			Point point = list.get(i);
			res++;
			while (i + 1 < list.size() && point.right >= list.get(i + 1).left){  //  尽量去缩进
				point.right = Math.min(point.right, list.get(i + 1).right);
				i++;
			}
		}
		return res;
    }
}
class Point{
	long left;
	long right;

	public Point(long left, long right) {
		this.left = left;
		this.right = right;
	}
}

435. 无重叠区间

class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
		ArrayList<Point> list = new ArrayList<>();
		for (int[] interval : intervals) {
			list.add(new Point(interval[0], interval[1]));
		}
		Collections.sort(list, new Comparator<Point>() {
			@Override
			public int compare(Point o1, Point o2) {
				if (o1.left != o2.left){
					return o1.left - o2.left;
				}
				return o1.right - o2.right;
			}
		});
		int res = 0;
		for (int i = 0; i < list.size(); i++) {
			Point point = list.get(i);
			while (i + 1 < list.size() && point.right > list.get(i + 1).left){
				res++;
				point.right = Math.min(point.right, list.get(i + 1).right);
				i++;
			}
		}
		return res;
    }
}

class Point{
	int left;
	int right;

	public Point(int left, int right) {
		this.left = left;
		this.right = right;
	}
}

OD 文艺表演

package com.llq.oo;

import java.util.*;

/**
 * @Author: Ronnie LEE
 * @Date: 2023/9/28 - 09 - 28 - 15:47
 * @Description: com.llq.oo
 * @version: 1.0
 */
public class show {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = Integer.parseInt(in.nextLine());
        List<Point> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int start = in.nextInt();
            int duration = in.nextInt();
            list.add(new Point(start, start + duration + 15));
        }
        for (Point point : list) {
            System.out.println(point.left + " " + point.right);
        }
        Collections.sort(list, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                if (o1.left < o2.left){
                    return o1.left - o2.left;
                }
                return o1.right - o2.right;
            }
        });
        int res = 0;
        for (int i = 0; i < list.size(); i++) {
            res++;
            Point point = list.get(i);
            while (i + 1 < list.size() && point.right > list.get(i + 1).left){
                point.right = Math.min(point.right, list.get(i + 1).right);
                i++;
            }
        }
        System.out.println(res);
    }
}

class Point{
    int left;
    int right;

    public Point(int left, int right) {
        this.left = left;
        this.right = right;
    }
}

2. 扩

OD:185. 路灯照明问题

难点:控制左右界

  • 左:>= 0
  • 右:(count - 1) * 100;
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static List<Tree> res = new ArrayList<>();

    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
        int count = Integer.parseInt(in.nextLine());
        int RLen = (count - 1) * 100;
        List<Tree> trees = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            long temp = in.nextLong();
            long left = Math.max(100 * i - temp, 0);
            long right = Math.min(100 * i + temp, RLen);

            trees.add(new Tree(left, right));
        }
        Collections.sort(trees, (o1, o2) -> {
            if (o1.left != o2.left){
                return Long.compare(o1.left, o2.left);
            }
            return Long.compare(o1.right, o2.right);
        });
        long sum = (count - 1) * 100;
        for (int i = 0; i < trees.size(); i++) {
            Tree tree = trees.get(i);
            while (i + 1 < trees.size() && tree.right >= trees.get(i + 1).left){
                tree.right = Math.max(tree.right, trees.get(i + 1).right);
                i++;
            }
            sum -= tree.right - tree.left;
        }
        System.out.println(sum);


    }
}

class Tree{
    long left;
    long right;

    public Tree(long left, long right) {
        this.left = left;
        this.right = right;
    }
}

3. 先缩再扩

OD230:区间交集

import java.util.Scanner;
import java.util.*;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static List<Num> res = new ArrayList<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Num> list = new ArrayList<>();
        int count = Integer.parseInt(in.nextLine());
        for (int i = 0; i < count; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            list.add(new Num(a, b));
        }
        Collections.sort(list, (o1, o2) -> o1.left - o2.left);

        for (int i = 0; i < list.size(); i++) {
            Num num = list.get(i);
            for (int j = i + 1; j < list.size(); j++) {
                Num num1 = list.get(j);
                if (num.right >= num1.left){
                    isCommon(list.get(i), list.get(j));
                }else {
                    break;
                }

            }
        }

        if (res.size() == 0){
            System.out.println("None");
            return;
        }
        Collections.sort(res, (o1, o2) -> o1.left - o2.left);
          for (int i = 0; i < res.size(); i++) {
            Num now = res.get(i);
            while (i + 1 < res.size() && res.get(i + 1).left <= now.right){
                now.right = Math.max(now.right, res.get(i + 1).right);
                i++;
            }
            System.out.println(now.left + " " + now.right);
        }

    }
    public static void isCommon(Num num1, Num num2){
        int left = Math.max(num1.left, num2.left);
        int right = Math.min(num1.right, num2.right);
        res.add(new Num(left,right));
    }
}

class Num {
    int left;
    int right;

    public Num(int left, int right) {
        this.left = left;
        this.right = right;
    }
}
posted @ 2023-09-28 12:58  爱新觉罗LQ  阅读(6)  评论(0编辑  收藏  举报