You are given an elevation map represented as an integer array heights where heights[i] represents the height of the terrain at index i. The width at each index is 1.
You are also given two integers volume and k. volume units of water will fall at index k.
Water first drops at index k and rests on top of the highest terrain or water at that index. Then, it flows according to these rules:
- If the droplet would eventually fall by moving left, then move left
- Otherwise, if the droplet would eventually fall by moving right, then move right
- Otherwise, rise to its current position
Here, "eventually fall" means that the droplet will eventually be at a lower level if it moves in that direction. Level means the height of terrain plus any water in that column.
Note: We assume there is infinitely high terrain on both sides out of bounds. Each unit of water must be in exactly one block.
Input & Output
Constraints
- 1 ≤ heights.length ≤ 100
- 0 ≤ heights[i] ≤ 99
- 0 ≤ volume ≤ 2000
- 0 ≤ k < heights.length