美文网首页
算法-面试题系列 - 二维数组装水问题

算法-面试题系列 - 二维数组装水问题

作者: 蒋斌文 | 来源:发表于2021-05-24 22:53 被阅读0次

算法-面试题系列 - 二维数组装水问题

如果给你一个二维数组,每一个值表示这一块地形的高度

求整块地形能装下多少水。

image-20210524221821025
image-20210524223231995 image-20210524224024795 image-20210524224641019 image-20210524225002663
public class TrappingRainWaterII {

   public static class Node {
      public int value;
      public int row;
      public int col;

      public Node(int v, int r, int c) {
         value = v;
         row = r;
         col = c;
      }

   }

   public static class NodeComparator implements Comparator<Node> {

      @Override
      public int compare(Node o1, Node o2) {
         return o1.value - o2.value;
      }

   }

   public static int trapRainWater(int[][] heightMap) {
      if (heightMap == null || heightMap.length == 0 || heightMap[0] == null || heightMap[0].length == 0) {
         return 0;
      }
      int N = heightMap.length;
      int M = heightMap[0].length;
      // isEnter[i][j] == true  之前进过
      //  isEnter[i][j] == false 之前没进过
      boolean[][] isEnter = new boolean[N][M];
      // 小根堆
      PriorityQueue<Node> heap = new PriorityQueue<>(new NodeComparator());
      for (int col = 0; col < M - 1; col++) {
         isEnter[0][col] = true;
         heap.add(new Node(heightMap[0][col], 0, col));
      }
      for (int row = 0; row < N - 1; row++) {
         isEnter[row][M - 1] = true;
         heap.add(new Node(heightMap[row][M - 1], row, M - 1));
      }
      for (int col = M - 1; col > 0; col--) {
         isEnter[N - 1][col] = true;
         heap.add(new Node(heightMap[N - 1][col], N - 1, col));
      }
      for (int row = N - 1; row > 0; row--) {
         isEnter[row][0] = true;
         heap.add(new Node(heightMap[row][0], row, 0));
      }
      
      
      
      
      int water = 0; // 每个位置的水量,累加到water上去
      int max = 0; // 每个node在弹出的时候,如果value更大,更新max,否则max的值维持不变
      while (!heap.isEmpty()) {
         Node cur = heap.poll();
         max = Math.max(max, cur.value);
         int r = cur.row;
         int c = cur.col;
         if (r > 0 && !isEnter[r - 1][c]) { // 如果有上面的位置并且上面位置没进过堆
            water += Math.max(0, max - heightMap[r - 1][c]);
            isEnter[r - 1][c] = true;
            heap.add(new Node(heightMap[r - 1][c], r - 1, c));
         }
         if (r < N - 1 && !isEnter[r + 1][c]) {
            water += Math.max(0, max - heightMap[r + 1][c]);
            isEnter[r + 1][c] = true;
            heap.add(new Node(heightMap[r + 1][c], r + 1, c));
         }
         if (c > 0 && !isEnter[r][c - 1]) {
            water += Math.max(0, max - heightMap[r][c - 1]);
            isEnter[r][c - 1] = true;
            heap.add(new Node(heightMap[r][c - 1], r, c - 1));
         }
         if (c < M - 1 && !isEnter[r][c + 1]) {
            water += Math.max(0, max - heightMap[r][c + 1]);
            isEnter[r][c + 1] = true;
            heap.add(new Node(heightMap[r][c + 1], r, c + 1));
         }
      }
      return water;
   }

}

相关文章

  • 算法-面试题系列 - 二维数组装水问题

    算法-面试题系列 - 二维数组装水问题 如果给你一个二维数组,每一个值表示这一块地形的高度 求整块地形能装下多少水。

  • 【数据结构与算法】复杂度知识

    什么是算法? 算法是用于解决特定问题的一系列的执行步骤。 以下算法是为了解决两数相加的问题。 以下算法是为了解决 ...

  • 算法-面试题系列 - 🏅🏅🏅 数组累加和问题三连 🏅🏅🏅

    算法-面试题系列 - ??? 数组累加和问题三连 ??? 题目一 arr[]数组,数组元素全部都是正数,给定sum...

  • 【恋上数据结构与算法一】(一)复杂度

    斐波那契数 1、什么是算法 ◼ 算法是用于解决特定问题的一系列的执行步骤 ◼ 使用不同算法,解决同一个问题,效率可...

  • 岛屿问题

    岛屿系列题目的核心考点就是用 DFS/BFS 算法遍历二维数组。本文分析DFS算法。 一、框架 因为二维矩阵本质上...

  • 【剑指offer-双指针】

    导读 算法 | 双指针套路总结 常用的双指针技巧 算法与数据结构基础 - 双指针 目录: 面试题04. 二维数组中...

  • 算法时间复杂度学习

    算法时间复杂度学习 1. 算法 算法:是用于解决特定问题的一系列的执行步骤。 举例: 简单的求两数之和,以及求n个...

  • 复杂度

    1、什么是算法 算法就是解决特定问题的一系列执行步骤。下面用两种不同的算法来实现获取第n个斐波那契数,在讲这个例子...

  • 剑指offer第二版-4.二维数组中的查找

    本系列导航:剑指offer(第二版)java实现导航帖 面试题4:二维数组中的查找 题目要求:一个二维数组中,每一...

  • Swap Nodes in Pairs

    标签: C++ 算法 LeetCode 链表 每日算法——leetcode系列 问题 Swap Nodes in ...

网友评论

      本文标题:算法-面试题系列 - 二维数组装水问题

      本文链接:https://www.haomeiwen.com/subject/jlifsltx.html