Rain Kept Between Walls
Updated
Problem statement
You are given a row of walls. Wall i is 1 unit wide and walls[i] units tall. The walls stand side by side with no gaps.
Rain falls on the whole row. Above wall i, water rises to the lower of two heights:
- the tallest wall from the first wall up to wall
i, and - the tallest wall from wall
iup to the last wall.
So wall i keeps that level minus walls[i] units of water. Water runs off both ends of the row, so a wall with nothing taller on one side keeps no water.
Return the total units of water kept on top of all the walls.
Function
rainKept(walls: int[]) → intExamples
Example 1
walls = [3,0,2,0,4]return = 7The tallest wall on the left of the middle part is 3 and on the right is 4, so water rises to the lower one, level 3. Above the walls of height 0, 2 and 0 that is 3 + 1 + 3 = 7 units.
Example 2
walls = [5,4,1,2]return = 1Only the wall of height 1 holds water. Its tallest wall on the left is 5 and on the right is 2, so the water rises to 2 and holds 1 unit. The wall of height 4 has nothing taller than 4 on its right, so it holds nothing. The answer is 1.
Example 3
walls = [0,3,0,1,0]return = 1The first 0 has no wall on its left, so water runs off that end. The 0 between 3 and 1 fills up to 1, the lower side: 1 unit. The last 0 has no wall on its right, so it holds nothing. The answer is 1.
Constraints
1 <= walls.length <= 10^40 <= walls[i] <= 10^4
More DocuSign Phone Screen questions
- Symmetric Binary TreePHONE SCREEN · Seen Oct 2026
- Union Area of Two RectanglesPHONE SCREEN · Seen Oct 2026
- Execute Cursor-Based Text Editor OperationsPHONE SCREEN · Seen Sep 2026
- Minimize Array Sum After K Halving OperationsPHONE SCREEN · Seen Sep 2026
- Fizz and Buzz Words From 1 to nPHONE SCREEN · Seen Sep 2026
- Two Positions That Add Up to a TargetPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Rebuild a Trip From Flight TicketsPHONE SCREEN · Seen Apr 2019
- Find the Words for Each PrefixPHONE SCREEN · Seen Sep 2018
Similar questions at other companies
- Rain Water Held Between BarsAdobe · PHONE SCREENAdobe interview questions
- Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Trapping Rain WaterGoogle · OAGoogle interview questions
- Trapping Rain WaterBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Trapping Rain WaterOracle · PHONE SCREENOracle interview questions
- Trapping Rain WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
- Trapping Rain WaterKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Trapping Rain WaterNuro · ONSITE INTERVIEWNuro interview questions
- Trapping Rain WaterRupeek · ONSITE INTERVIEW