Pour Water - Problem

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

Example 1 — Basic Water Flow
$ Input: heights = [2,1,1,2,1,2,2], volume = 4, k = 3
Output: [2,3,3,2,2,2,2]
💡 Note: Water drops at index 3 (height 2). First drop flows left to index 1 (height 1+1=2). Second drop flows left to index 2 (height 1+1=2). Third drop flows left to index 1 (height 2+1=3). Fourth drop flows left to index 2 (height 2+1=3).
Example 2 — Water Stays at Drop Position
$ Input: heights = [1,2,3,4], volume = 2, k = 2
Output: [1,2,5,4]
💡 Note: Water drops at index 2 (height 3). Both drops cannot flow left (heights increase) or right (height increases), so they stay at position 2: 3+2=5.
Example 3 — Water Flows Right
$ Input: heights = [3,1,3], volume = 5, k = 0
Output: [3,6,3]
💡 Note: Water drops at index 0 (height 3). Cannot flow left (out of bounds), but can flow right to index 1 (lower height). All 5 units flow right and settle at index 1: 1+5=6.

Constraints

  • 1 ≤ heights.length ≤ 100
  • 0 ≤ heights[i] ≤ 99
  • 0 ≤ volume ≤ 2000
  • 0 ≤ k < heights.length

Visualization

Tap to expand
Pour Water - Optimized Simulation INPUT Elevation Map Visualization 2 1 1 2 k=3 1 2 2 0 1 2 3 4 5 6 Input Parameters heights = [2,1,1,2,1,2,2] volume = 4 k = 3 (drop position) 4 water drops at index 3 ALGORITHM STEPS 1 Drop Water at k Start at position k=3 2 Try Flow Left Find lowest point left of k 3 Try Flow Right If left blocked, try right 4 Fill Position Increment height at dest Simulation Trace Drop 1: k=3 --> left --> idx 1 [2,2,1,2,1,2,2] Drop 2: k=3 --> left --> idx 2 [2,2,2,2,1,2,2] Drop 3: k=3 --> right --> idx 4 [2,2,2,2,2,2,2] Drop 4: k=3 --> left --> idx 1 [2,3,2,2,2,2,2] Final: [2,3,3,2,2,2,2] FINAL RESULT Final State (with water) 2 3 3 2 2 2 2 0 1 2 3 4 5 6 Terrain Water Output Array [2, 3, 3, 2, 2, 2, 2] OK - All 4 drops placed Key Insight: Water follows gravity rules: it tries to flow LEFT first to find a lower position. If no lower point exists on the left, it tries RIGHT. If both sides are blocked (same or higher), water stays at current position k. The optimized simulation tracks the closest lower point efficiently. TutorialsPoint - Pour Water | Optimized Simulation Approach
Asked in
Google 15 Facebook 12 Amazon 8
23.4K Views
Medium Frequency
~25 min Avg. Time
580 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen