PriorityQueue的使用

简介
  PriorityQueue 是 Java 中基于优先级堆实现的一个队列,可以用来存储一组元素,并按照一定的优先级顺序访问这些元素,优先级高的元素会被先访问。其类型和优先级可自行定义。

案例

        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });

        priorityQueue.offer(3);
        priorityQueue.offer(1);
        priorityQueue.offer(2);

        while (!priorityQueue.isEmpty()) {
            System.out.println(priorityQueue.poll());
        }

		输出结果:
		1
		2
		3

  该案例创建了一个Integer类型的最小堆,每当插入一个元素时,最小的元素必定在最顶部。该案例中插入元素使用了offer方法,除此之外还有一个add方法,两者的区别是:
●add
  如果队列未满,则将指定的元素插入到队列的尾部。
  如果队列已满,则抛出IllegalStateException异常,也就是说,此方法不允许插入操作失败。
●offer
  如果队列未满,则将指定的元素插入到队列的尾部。
  如果队列已满,则直接返回false,不会抛出任何异常。

实战
接雨水 II
给你一个 m x n 的矩阵,其中的值均为非负整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。

示例 1:
在这里插入图片描述

输入: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
输出: 4
解释: 下雨后,雨水将会被上图蓝色的方块中。总的接雨水量为1+2+1=4。

示例 2:
在这里插入图片描述
输入: heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
输出: 10

参考:

    public static int trapRainWater(int[][] heightMap) {
        int m = heightMap.length;
        int n = heightMap[0].length;

        if (m < 3 || n < 3) {
            return 0;
        }

        boolean[][] visit = new boolean[m][n];

        // 最小堆
        PriorityQueue<int[]> p = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        for (int i = 0; i < m; i++) {
            p.offer(new int[]{i * n, heightMap[i][0]});
            visit[i][0] = true;
        }

        for (int i = 0; i < m; i++) {
            p.offer(new int[]{i * n + n -1, heightMap[i][n - 1]});
            visit[i][n - 1] = true;
        }

        for (int j = 1; j < n -1; j++) {
            p.offer(new int[]{j, heightMap[0][j]});
            visit[0][j] = true;
        }

        for (int j = 1; j < n -1; j++) {
            p.offer(new int[]{(m - 1) * n + j, heightMap[m - 1][j]});
            visit[m - 1][j] = true;
        }

        int num = 0;
        int[] direction = new int[]{-1, 0, 1, 0, -1};
        int x, y;
        while (!p.isEmpty())  {
            int[] q = p.poll();
            for (int i = 0; i < 4; i++) {
                y = q[0] / n + direction[i];
                x = q[0] % n + direction[i + 1];
                if (y > 0 && y < m && x > 0 && x < n && !visit[y][x]) {
                    if (q[1] > heightMap[y][x]) {
                        num += q[1] - heightMap[y][x];
                    }
                    visit[y][x] = true;
                    p.offer(new int[]{y * n + x, Math.max(q[1], heightMap[y][x])});
                }
            }
        }

        return num;
    }

  该案例中创建了一个二维数组的最小堆,堆的优先级顺序以p[1]大小进行排序,也就是接雨水后最外围的高度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值