LeetCode 42. Trapping Rain Water

本文介绍了一种方法,用于计算给定高度数组在降雨后的积水量。通过找到最大高度索引来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.


To solve the problem. the key point to find the maxHeight  index.


#include <vector>
#include <iostream>
#include <climits>
using namespace std;

/*
  Given n non-negative integers representing an elevation map where the width of each bar is 1.
  compute how much water it is able to trap after raining.
  For example:
  Given [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1], return 6.
*/

int trap(vector<int>& height) {
  int maxHeight = 0;
  int maxIndex = 0;
  for(int i = 0; i < height.size(); ++i) {
    if(maxHeight < height[i]) {
      maxHeight = height[i];
      maxIndex = i;
    }
  }
  int water = 0;
  int maxSoFar = 0;
  for(int i = 0; i < <strong>maxIndex</strong>; ++i) {
    if(height[i] < maxSoFar) water += (maxSoFar - height[i]); // less then maxSoFar can accumulate water.
    else maxSoFar = height[i];
  }

  maxSoFar = 0;
  for(int i = height.size() - 1; i > <strong>maxIndex</strong>; --i) {
    if(height[i] < maxSoFar) water += (maxSoFar - height[i]);  // less then maxSoFar can accumulate water.
    else maxSoFar = height[i];
  }
  return water;
}

int main(void) {
  vector<int> nums{0, 1, 0, 3, 1, 0, 1, 3, 2, 1, 2, 1};
  cout << trap(nums) << endl;
}

Another interesting variation! Trapping rain water in matrix

/*
  Suppose given a matrix
  {{0, 2, 5},
   {5, 4, 5},
   {5, 5, 5}};
  In this matrix, the max water can be trapped is: 0;
*/
#include "header.h"
using namespace std;

class Solution {
private:
  vector< vector<int> > matrix;

public:
  Solution(vector< vector<int> >& input) {
    matrix = input;
  }

  int trappWaterinMatrix() {
    if(matrix.size() == 0 || matrix[0].size() == 0) return 0;
    int rows = matrix.size();
    int cols = matrix[0].size();
    struct posIndex {
      int value;
      int row;
      int col;
      posIndex(int v, int r, int c) : value(v), row(r), col(c) {}
      bool operator < (posIndex b) const {
        return value > b.value;
      }
    };

   priority_queue<posIndex, vector<posIndex> > minHeap;
    vector< vector<bool> > visited(rows, vector<bool>(cols, false));
    // we need to first get the minimum value from the border.
    // first row and last row
    for(int i = 0; i < cols; ++i) {
      minHeap.push(posIndex(matrix[0][i], 0, i));
      visited[0][i] = true;
      minHeap.push(posIndex(matrix[rows - 1][i], rows - 1, i));
      visited[rows - 1][i] = true;
    }
    // first column and last column
    for(int i = 1; i < rows; ++i) {
      minHeap.push(posIndex(matrix[i][0], i, 0));
      visited[i][0] = true;
      minHeap.push(posIndex(matrix[i][cols -1], i, cols -1));
      visited[i][cols - 1] = true;
    }
    int waterTrapped = 0;
    // bfs traversal the matrix to caculate the trapped water.
    vector< vector<int> > dir{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
    while(!minHeap.empty()) {
      auto curr = minHeap.top(); minHeap.pop();
      for(int i = 0; i < dir.size(); ++i) {
        posIndex next(0, 0, 0);
        next.row = curr.row + dir[i][0];
        next.col = curr.col + dir[i][1];
        if(next.row >= 0 && next.row < rows && next.col >= 0 && next.col < cols && !visited[next.row][next.col]) {
          visited[next.row][next.col] = true;
          next.value = max(curr.value, matrix[next.row][next.col]);
          minHeap.push(next);
          waterTrapped += max(0, curr.value - matrix[next.row][next.col]);
        }
      }
    }
    return waterTrapped;
  }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值