矩形面积 II

矩形面积 II

Tags
Column
class Solution {
    public int rectangleArea(int[][] rectangles) {
        long area = 0;
        int id = 0;
        PriorityQueue<Item> pq = new PriorityQueue<>((o1, o2) -> o1.x - o2.x);
        Map<Integer, Item> map = new HashMap<>();

        for (int[] rect : rectangles) {
            //将一个矩形拆分为2个item
            //              (x2,y2)
            //
            // (x1,y1)

            //转换为:
            //x1, y1, y2, in  (左边入)
            //x2, y1, y2, out (右边出)
            //id 是方便后续从map中移除
            pq.add(new Item(id, rect[0], rect[1], rect[3], FLAG_IN));
            pq.add(new Item(id, rect[2], rect[1], rect[3], FLAG_OUT));
            id++;
        }

        while (!pq.isEmpty()) {
            int x1 = pq.peek().x;
            //读取 pq中所有 x值为x1的, 并且更新map (in -添加;  out- 移除)
            updateMap(x1, map, pq);

            //已经读取到最后一个x位置了。次数无法再计算面积了, 所以直接退出
            if (pq.isEmpty()) {
                break;
            }

            int x2 = pq.peek().x;
//            System.out.println(x2);
            PriorityQueue<Item> pq1 = new PriorityQueue<>((o1, o2) -> o1.y1 - o2.y1);
//            for (Item it:map.values()){
//                System.out.println("x = "+it.x);
//                System.out.println("y1 = "+ it.y1);
//                System.out.println("y2 = "+ it.y2);
//                System.out.println("x2 = "+ x2);
//            }
//            System.out.println("__________________________");
            pq1.addAll(map.values());

            long tempArea = getArea(pq1, x1, x2);
            area += tempArea;
        }

        return (int) (area % 1000000007);
    }

    private void updateMap(int x1, Map<Integer, Item> map, PriorityQueue<Item> pq) {
//        System.out.println("x1 = "+x1);
        while (!pq.isEmpty() && pq.peek().x == x1) {
            Item item = pq.poll();
            if (item.flag == FLAG_IN) {
//                System.out.println("flag_in :" + item.x);
                map.put(item.id, item);
            } else {
//                System.out.println("flag_out :" + item.x);
                map.remove(item.id);
            }
        }
    }

    private long getArea(PriorityQueue<Item> pq1, int x1, int x2) {
        long area = 0;
        List<Item> list = new ArrayList<>();

        //合并item后,放到list中。
        while (!pq1.isEmpty()) {
            Item item = pq1.poll();
            merge(list, item.y1, item.y2);
        }

        //基于合并后的item,计算area
        for (Item item : list) {
            area += (long) (item.y2 - item.y1) * (x2 - x1);
        }

        return area;
    }

    private void merge(List<Item> list, int y1, int y2) {
        if (list.isEmpty()) {
            list.add(new Item(y1, y2));
            return;
        }

        Item last = list.get(list.size() - 1);
        if (last.y2 > y1) {
            last.y2 = Math.max(last.y2, y2);
        } else {
            list.add(new Item(y1, y2));
        }
    }

    private final static int FLAG_IN = 1;
    private final static int FLAG_OUT = -1;

    static class Item {
        public int id;
        public int x;
        public int y1;
        public int y2;
        public int flag;

        public Item(int y1, int y2) {
            this.y1 = y1;
            this.y2 = y2;
        }

        public Item(int id, int x, int y1, int y2, int flag) {
            this.id = id;
            this.x = x;
            this.y1 = y1;
            this.y2 = y2;
            this.flag = flag;
        }

        @Override
        public String toString() {
            return "Item{" +
                    "id=" + id +
                    ", x=" + x +
                    ", y1=" + y1 +
                    ", y2=" + y2 +
                    ", flag=" + flag +
                    '}';
        }
    }
}