Rainwater Trapping
Function Name: trapRainwater
Description
Write a function called trapRainwater that calculates how much rainwater can be trapped between the given heights of bars. You are given an array that represents an elevation map where the width of each bar is 1. The function should compute how much water is trapped after raining. Bars of width 1 and non-negative integers are represented in the array where each value represents the height of the bar at that position.
Requirements
- The function should take an array of integers as its parameter.
- You should not use any built-in functions that directly solve the problem, like third-party algorithm implementations.
- Optimize the function for time complexity where possible.
- Write clean and readable code with proper variable names and comments.
- Do not mutate the original array.
- Handle the edge case where the input array is empty, in which case the function should return 0.
- Assume the input array's length does not exceed 10,000 elements.
Examples
trapRainwater([0,1,0,2,1,0,1,3,2,1,2,1]) should return 6.trapRainwater([4,2,0,3,2,5]) should return 9.trapRainwater([]) should return 0.
Links
Unique Paths in a Grid
Tue Oct 08 2024
Write a function that calculates the number of unique paths from the top-left corner to the bottom-right corner of a m x n grid. You are only allowed to move either down or right at any point in time.The function should take two parameters which are the dimensions of the grid (m for rows and n for columns).Assume both m and n will be greater than or equal to 1.
Matrix Spiral Copy
Thu Oct 10 2024
Write a function that takes a 2D array (matrix) as its parameter and returns a list of its elements in a spiral order.You should start from the top-left corner of the matrix, proceed to the right, and continue spiraling into the center of the matrix.The spiral order should proceed with the pattern: right -> down -> left -> up, repeating until all elements have been visited.