• 数组容器装水问题解法


    问题描述一

            给定一个数组arr,已知其中所有的值都是非负的,将这个数组看作一个容器, 请返回容器能装多少水比如,arr = {3,1,2,5,2,4},根据值画出的直方图就是容器形状,该容 器可以装下5格水再比如,arr = {4,5,1,3,2},该容器可以装下2格水。      

    思路

              这题的整体解题思路是:数组中的任意一个数X,计算出X左侧数组中的最大值记为 leftMax,X右侧数组中的最大值记为 rightMax。根据公式 Math.max(Math.min(leftMax,rightMax)-        X,0) 可以计算出数组中每个位置可以装下水的水量,全部相加即为最终答案。

            本题在计算X左右两侧数组中的最大值时,可以进行不断的优化。

            优化一:在X位置的左右两侧每次分别使用for循环查找两侧的最大值。本题的时间复杂度为:O(N^2)。

    1. public static int water1(int[] arr){
    2. if (arr==null || arr.length<2){
    3. return 0;
    4. }
    5. int N = arr.length;
    6. int water = 0;
    7. for (int i = 1;i1;i++){
    8. int leftMax = Integer.MIN_VALUE;
    9. for (int j = 0;j
    10. leftMax = Math.max(leftMax,arr[j]);
    11. }
    12. int rightMax = Integer.MIN_VALUE;
    13. for (int j = i+1;j
    14. rightMax = Math.max(rightMax,arr[j]);
    15. }
    16. water += Math.max(Math.min(leftMax,rightMax)-arr[i],0);
    17. }
    18. return water;
    19. }

            优化二:使用预处理数组,提前计算出X位置左侧与右侧最大值记录在数组中。在遍历过程中直接查找预处理数组中提前计算的最大值。本题的时间复杂度由原来的O(N^2)降为O(N)。

    1. public static int water2(int[] arr){
    2. if (arr == null || arr.length < 2){
    3. return 0;
    4. }
    5. int N = arr.length;
    6. int[] leftMax = new int[N];
    7. leftMax[0] = arr[0];
    8. for (int i =1;i
    9. leftMax[i] = Math.max(leftMax[i-1],arr[i]);
    10. }
    11. int[] rightMax = new int[N];
    12. rightMax[N-1] = arr[N-1];
    13. for (int i = N-2;i>=0;i--){
    14. rightMax[i] = Math.max(rightMax[i+1],arr[i]);
    15. }
    16. int water = 0;
    17. for (int i = 1;i1;i++){
    18. water += Math.max(Math.min(leftMax[i-1],rightMax[i+1])-arr[i],0);
    19. }
    20. return water;
    21. }

             优化三:仍然使用预处理数组,但是我们只是用一个预处理数组用于记录右侧数组的最大值,这是因为我们数组在从左遍历的过程中我们可以使用一个变量通过不断更新的方式来记录左侧数组的最大值,这样就节省了一个数组。

    1. public static int water3(int[] arr){
    2. if (arr == null ||arr.length<2){
    3. return 0;
    4. }
    5. int N = arr.length;
    6. int[] rightMaxs = new int[N];
    7. rightMaxs[N-1] = arr[N-1];
    8. for (int i =N-2;i>=0;i--){
    9. rightMaxs[i] = Math.max(rightMaxs[i+1],arr[i]);
    10. }
    11. int water = 0;
    12. int leftMax = arr[0];
    13. for (int i = 1;i1;i++){
    14. water += Math.max(Math.min(leftMax,rightMaxs[i+1])-arr[i],0);
    15. leftMax = Math.max(leftMax,arr[i]);
    16. }
    17. return water;
    18. }

            优化四: 不使用预处理数组,我们采用双指针的方式。数组的头和尾不可能形成容器直接跳过,记录一个左边最大值,一个右边最大值,如果左小于右先结算左边水量,如果右小于左先结算右边的水量,哪边是瓶颈就先结算哪边的水量。

    1. public static int water4(int[] arr){
    2. if (arr == null ||arr.length<2){
    3. return 0;
    4. }
    5. int N = arr.length;
    6. int L = 1;
    7. int leftMax = arr[0];
    8. int R = N-2;
    9. int rightMax = arr[N-1];
    10. int water = 0;
    11. while (L<=R){
    12. if (leftMax <= rightMax){
    13. water += Math.max(0,leftMax-arr[L]);
    14. leftMax = Math.max(leftMax,arr[L++]);
    15. }else {
    16. water += Math.max(0,rightMax-arr[R]);
    17. rightMax = Math.max(rightMax,arr[R--]);
    18. }
    19. }
    20. return water;
    21. }

    问题描述二

            如果给你一个二维数组,每一个值表示这一块地形的高度,求整块地形能装下多少水。

    思路 

            利用小根堆求解。由于四个边都不能形成装水区域,首先把四个边加入到小根堆中,在准备一个布尔类型的二维数组,记录所有进过小根堆的位置,进过一次后就不允许在进入小根堆了。准备一个变量max用于记录所在湖的最大高度。此时第一次弹出的一定是边界上最小的位置,以此位置为根据地,把根据地的高度记录在max中,把它四周未加入过小根堆的点加入小根堆,查找周边是否有比它地势低的点,就可以计算出地势低点的水位高度。每次弹出一个元素,查看当前元素的值是否比之前的最大值还大,若成立则更新最大值,然后收集他相邻的上下左右四个位置的水位,然后把四个位置放入小根堆,重复上述过程直到小根堆变为空。

    代码

    1. public static class Node {
    2. public int value;
    3. public int row;
    4. public int col;
    5. public Node(int v, int r, int c) {
    6. value = v;
    7. row = r;
    8. col = c;
    9. }
    10. }
    11. public static class NodeComparator implements Comparator {
    12. @Override
    13. public int compare(Node o1, Node o2) {
    14. return o1.value - o2.value;
    15. }
    16. }
    17. public static int trapRainWater(int[][] heightMap) {
    18. if (heightMap == null || heightMap.length == 0 || heightMap[0] == null || heightMap[0].length == 0) {
    19. return 0;
    20. }
    21. int N = heightMap.length;
    22. int M = heightMap[0].length;
    23. boolean[][] isEnter = new boolean[N][M];
    24. PriorityQueue heap = new PriorityQueue<>(new NodeComparator());
    25. for (int col = 0; col < M - 1; col++) {
    26. isEnter[0][col] = true;
    27. heap.add(new Node(heightMap[0][col], 0, col));
    28. }
    29. for (int row = 0; row < N - 1; row++) {
    30. isEnter[row][M - 1] = true;
    31. heap.add(new Node(heightMap[row][M - 1], row, M - 1));
    32. }
    33. for (int col = M - 1; col > 0; col--) {
    34. isEnter[N - 1][col] = true;
    35. heap.add(new Node(heightMap[N - 1][col], N - 1, col));
    36. }
    37. for (int row = N - 1; row > 0; row--) {
    38. isEnter[row][0] = true;
    39. heap.add(new Node(heightMap[row][0], row, 0));
    40. }
    41. int water = 0;
    42. int max = 0;//每次node在弹出的时候,如果value更大,更新max,否则max的值维持不变
    43. while (!heap.isEmpty()) {
    44. Node cur = heap.poll();
    45. max = Math.max(max, cur.value);
    46. int r = cur.row;
    47. int c = cur.col;
    48. if (r > 0 && !isEnter[r - 1][c]) {
    49. water += Math.max(0, max - heightMap[r - 1][c]);
    50. isEnter[r - 1][c] = true;
    51. heap.add(new Node(heightMap[r - 1][c], r - 1, c));
    52. }
    53. if (r < N - 1 && !isEnter[r + 1][c]) {
    54. water += Math.max(0, max - heightMap[r + 1][c]);
    55. isEnter[r + 1][c] = true;
    56. heap.add(new Node(heightMap[r + 1][c], r + 1, c));
    57. }
    58. if (c > 0 && !isEnter[r][c - 1]) {
    59. water += Math.max(0, max - heightMap[r][c - 1]);
    60. isEnter[r][c - 1] = true;
    61. heap.add(new Node(heightMap[r][c - 1], r, c - 1));
    62. }
    63. if (c < M - 1 && !isEnter[r][c + 1]) {
    64. water += Math.max(0, max - heightMap[r][c + 1]);
    65. isEnter[r][c + 1] = true;
    66. heap.add(new Node(heightMap[r][c + 1], r, c + 1));
    67. }
    68. }
    69. return water;
    70. }

    时间复杂度

            遍历二维数组中的全部数字需要时间复杂度为:O(N*M)

            小根堆的时间复杂度为O(logk),其中k最大为N*M。

            综上所述,该题的时间复杂度为:O(N*M*log(N*M)) 

  • 相关阅读:
    kotlin coroutine源码解析之Job启动流程
    java计算机毕业设计环巢湖区域旅游网站MyBatis+系统+LW文档+源码+调试部署
    OKR,为什么我喜欢他们
    详解linux多线程——互斥锁、条件变量、读写锁、自旋锁、信号量
    把请求头信息添加到请求报文中,然后发送请求到淘宝,显示回复信息
    7.2 通过API创建新进程
    使用 HTTP Client 轻松进行 API 测试
    2.6一个小工具的使用snipaste
    机器人课程教师面对的困境有哪些(补充)
    nacos服务注册源码过程阅读
  • 原文地址:https://blog.csdn.net/z1171127310/article/details/127704037