Advertisement

Trapping Rain Water II - LeetCode 407 Solution

Trapping Rain Water II - Complete Solution Guide

Trapping Rain Water II is LeetCode problem 407, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an m x n integer matrix heightMap representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining . Example 1: Input: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]] Output: 4 Explanation: After the rain, water is trapped between the blocks. We have two small ponds 1 and 3 units trapped. The total volume of water trapped is 4. Example 2: Input: 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]] Output: 10

Detailed Explanation

The problem asks us to calculate the amount of water that can be trapped in a given 2D elevation map after it rains. The elevation map is represented by an `m x n` integer matrix `heightMap`, where `heightMap[i][j]` represents the height of each cell. The water can be trapped between cells of varying heights, similar to how water is trapped in a valley formed by mountains. The goal is to find the total volume of water trapped after the rain fills the depressions in the map.

Solution Approach

The solution employs a priority queue based approach combined with Breadth-First Search (BFS). We start by adding all boundary cells of the `heightMap` to a min-heap. Then, we iteratively extract the cell with the minimum height from the heap. For each extracted cell, we explore its unvisited neighbors. If a neighbor's height is less than the current cell's height, water is trapped, and the total trapped water is updated. The neighbor is then added to the priority queue with the height being the maximum of the current cell's height and the neighbor's actual height. This ensures that the water level propagates correctly throughout the map.

Step-by-Step Algorithm

  1. Step 1: Initialize a visited matrix to keep track of visited cells and a priority queue (min-heap) to store boundary cells.
  2. Step 2: Add all boundary cells to the priority queue along with their coordinates and mark them as visited.
  3. Step 3: Initialize a variable `total_water` to store the total trapped water.
  4. Step 4: While the priority queue is not empty, extract the cell with the minimum height.
  5. Step 5: For each unvisited neighbor of the extracted cell:
  6. Step 6: Mark the neighbor as visited.
  7. Step 7: Calculate the water trapped at the neighbor as `max(0, current_cell_height - neighbor_height)` and add it to `total_water`.
  8. Step 8: Add the neighbor to the priority queue with height as `max(current_cell_height, neighbor_height)` to propagate the water level.
  9. Step 9: Return the `total_water`.

Key Insights

  • Insight 1: The water level at any cell is determined by the minimum height of its surrounding boundary cells. The cells on the boundary cannot trap any water, so they form the starting point.
  • Insight 2: We can use a priority queue (min-heap) to process cells in increasing order of their height. This allows us to simulate the water filling process from the boundary inwards.
  • Insight 3: For each cell, we compare its height to the current water level (the height of the boundary cell). If the cell's height is lower, water gets trapped. The water level for the neighboring cells is then updated to the maximum of the current water level and their own height.

Complexity Analysis

Time Complexity: O(m*n*log(m*n))

Space Complexity: O(m*n)

Topics

This problem involves: Array, Breadth-First Search, Heap (Priority Queue), Matrix.

Companies

Asked at: ByteDance, Otter.ai, Palantir Technologies, Qualcomm, X.