Trapping Rain Water II Problem Statement

Given an M * N matrix where each cell's value represents its height in a 2-D elevation map, calculate the total volume of water that can be trapped after rainfall.

Input:

The first line contains an integer ‘T’ representing the number of test cases.
For each test case:
- The first line contains two integers 'M' and 'N', indicating the number of rows and columns respectively.
- The following 'M' lines, each containing 'N' space-separated integers, denote the height of each cell in the matrix.

Output:

For each test case, output a single integer representing the volume of water that can be trapped.
Print each result on a new line.

Example:

Input:
1
3 3
5 5 5
5 2 3
6 9 8
Output:
1
Explanation:

The given matrix forms a 2-D elevation map where, after rain, water is trapped leading to a total volume of 1 unit.

Constraints:

  • 1 <= T <= 10
  • 1 <= M,N <= 100
  • 1 <= matrix[i][j] <= 10^3

Note: You aren’t required to handle the printing of results; this is managed elsewhere. Your task is to implement the function to determine the trapped volume.

Be the first one to answer
Add answer anonymously...
MindTickle 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