Ctrl K

Droplets on the Terrain

Medium
The terrain is given by the array ground: ground[i] is the height of the land in column i, and each column is 1 wide. Beyond both ends of the array stand infinitely high walls. The level of a column is its land height plus any water already collected there. drops drops of water fall one by one onto column spot. Each drop first lands on top of column spot and then moves like this: - if, walking left over columns no higher than where it is, it can eventually get down to a level lower than that of spot, it goes left; - otherwise, if such a descent is possible to the right, it goes right; - otherwise it stays at spot. When a drop moves sideways, it settles in the column closest to spot that has the lowest level among the ones it can reach without ever climbing. Each drop fills one whole column and raises its level by 1. Return the array of levels after all the drops have fallen.
Example 1
ground=[3, 0, 2, 2, 1], drops=3, spot=2 → [3, 2, 2, 2, 2]

The first drop runs left into the dip at 1. The second still sees a way down to the left and settles at 1 as well. Now the left side is flat (level 2 up to the wall), so the third drop goes right and settles at position 4.

Example 2
ground=[2, 2, 2], drops=2, spot=1 → [3, 3, 2]

The ground is flat, so the first drop stays where it lands. After that position 1 is higher than its neighbours, and the second drop slides left.

Example 3
ground=[5], drops=4, spot=0 → [9]
Constraints
  • 1 ≤ len(ground) ≤ 100
  • 0 ≤ ground[i] ≤ 99
  • 0 ≤ drops ≤ 2000
  • 0 ≤ spot < len(ground)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
ground=[3, 0, 2, 2, 1], drops=3, spot=2
Expected
[3, 2, 2, 2, 2]

Run checks the first 3 tests in your browser. Submit checks all 123 tests on the server, hidden ones included.