Tap here to switch tabs
Problem
Submissions

Problem: Pour Water

Try to solve the Pour Water problem.

Statement

You are given an elevation map represented by an integer array heights where heights[i] represents the height of the terrain at index i. You are also given two integers volume and k, representing the total units of water to pour and the starting index where each unit of water is dropped, respectively.

Pour volume units of water onto the terrain, one unit at a time, at index k. Each water droplet behaves according to the following rules:

  1. The droplet first attempts to move left. It can flow to the adjacent left column if that column’s height is less than or equal to the current column’s height. It continues moving left until the terrain rises or it reaches the left boundary. If any visited column has an elevation strictly lower than the elevation at index k, the droplet settles at the position with the lowest elevation reached that is closest to index k.

  2. If moving left does not find a position with a lower elevation than index k, the droplet attempts to move right following the same principle. It continues moving right as long as the terrain does not rise and settles at the lowest position strictly lower than index k that is closest to index k.

  3. If neither moving left nor moving right results in a lower elevation, the droplet remains at index k.

After a droplet settles, the height at its resting position increases by 11.

Return the heights array after all volume units of water have been poured.

Constraints:

  • 1≤1 \leq heights.length ≤100\leq 100

  • 0≤0 \leq heights[i] ≤99\leq 99

  • 0≤0 \leq volume ≤2000\leq 2000

  • 0≤0 \leq k << heights.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Pour Water

Try to solve the Pour Water problem.

Statement

You are given an elevation map represented by an integer array heights where heights[i] represents the height of the terrain at index i. You are also given two integers volume and k, representing the total units of water to pour and the starting index where each unit of water is dropped, respectively.

Pour volume units of water onto the terrain, one unit at a time, at index k. Each water droplet behaves according to the following rules:

  1. The droplet first attempts to move left. It can flow to the adjacent left column if that column’s height is less than or equal to the current column’s height. It continues moving left until the terrain rises or it reaches the left boundary. If any visited column has an elevation strictly lower than the elevation at index k, the droplet settles at the position with the lowest elevation reached that is closest to index k.

  2. If moving left does not find a position with a lower elevation than index k, the droplet attempts to move right following the same principle. It continues moving right as long as the terrain does not rise and settles at the lowest position strictly lower than index k that is closest to index k.

  3. If neither moving left nor moving right results in a lower elevation, the droplet remains at index k.

After a droplet settles, the height at its resting position increases by 11.

Return the heights array after all volume units of water have been poured.

Constraints:

  • 1≤1 \leq heights.length ≤100\leq 100

  • 0≤0 \leq heights[i] ≤99\leq 99

  • 0≤0 \leq volume ≤2000\leq 2000

  • 0≤0 \leq k << heights.length