leetcode有关坐标的问题(line sweep)

  1. The Skyline Problem
    在这里插入图片描述
public List<int[]> getSkyline(int[][] buildings) {
    List<int[]> result = new ArrayList<>();
    List<int[]> height = new ArrayList<>();
    for(int[] b:buildings) {
        height.add(new int[]{b[0], -b[2]});
        height.add(new int[]{b[1], b[2]});
    }
    Collections.sort(height, (a, b) -> {
            if(a[0] != b[0]) 
                return a[0] - b[0];
            return a[1] - b[1];
    });
    Queue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));
    pq.offer(0);
    int prev = 0;
    for(int[] h:height) {
        if(h[1] < 0) {
            pq.offer(-h[1]);
        } else {
            pq.remove(h[1]);
        }
        int cur = pq.peek();
        if(prev != cur) {
            result.add(new int[]{h[0], cur});
            prev = cur;
        }
    }
    return result;
}
  • 优先队列:最小堆
  • 最大堆的定义
 Queue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));

或者

Queue<Integer> maxQ = new PriorityQueue<>(new Comparator<Integer>(){
            public int compare(Integer a, Integer b){
               return -a.compareTo(b);
            } 
        });

2.Perfect Rectangle
在这里插入图片描述

    public boolean isRectangleCover(int[][] rectangles) {
        // 面积“守恒”
        //只有四个点出现一次,出现一次的一定是四个角。其他点的个数为2或4
        HashSet<String> set = new HashSet<>();
        int areasum = 0;
        int xmin = Integer.MAX_VALUE, xmax = Integer.MIN_VALUE;
        int ymin = Integer.MAX_VALUE, ymax = Integer.MIN_VALUE;
        for(int[] rec : rectangles){
            xmin = Math.min(xmin, Math.min(rec[0], rec[2]));
            xmax = Math.max(xmax, Math.max(rec[0], rec[2]));
            ymin = Math.min(ymin, Math.min(rec[1], rec[3]));
            ymax = Math.max(ymax, Math.max(rec[3], rec[3]));
            areasum += (rec[2]- rec[0]) * (rec[3] - rec[1]);
            if(!set.add(rec[0] + " " + rec[1])) set.remove(rec[0] + " " + rec[1]);
            if(!set.add(rec[0] + " " + rec[3])) set.remove(rec[0] + " " + rec[3]);
            if(!set.add(rec[2] + " " + rec[1])) set.remove(rec[2] + " " + rec[1]);
            if(!set.add(rec[2] + " " + rec[3])) set.remove(rec[2] + " " + rec[3]);
        }
        if(set.size() != 4 || !set.contains(xmin + " " + ymin) || 
           !set.contains(xmin + " " + ymax) || !set.contains(xmax + " " + ymin)|| !set.contains(xmax + " " + ymax)) 
            return false;
        return areasum == (ymax - ymin) * (xmax - xmin);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值