Trapping Rain Water in 2-D Elevation Map

You're given an M * N matrix where each value represents the height of that cell on a 2-D elevation map. Your task is to determine the total volume of water that can be trapped after it rains.

Example:

Input:
matrix = [[5, 5, 5],
[5, 2, 3],
[6, 9, 8]]
Output:
1
Explanation:

The elevation map allows 1 unit volume of water to be trapped, as illustrated in the image.

Constraints:

  • 1 <= T <= 10
  • 1 <= M, N <= 100
  • 1 <= matrix[i][j] <= 10^3
  • Where ‘T’ is the number of test cases, ‘M’ and ‘N’ are the dimensions of the given matrix.
  • Matrix[i][j] is the height of the cell at the ‘i-th’ row and ‘j-th’ column.
  • Time Limit: 1 sec.
Note:
You don't need to print anything, as it has already been taken care of. Just implement the given function.
AnswerBot
2d

Calculate the total volume of water that can be trapped in a 2-D elevation map after rain.

  • Iterate over the matrix and find the maximum height on the borders.

  • Calculate the water trapped at each cell by...read more

Help your peers!
Add answer anonymously...
ION Group Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter