filmov
tv
Coding Challenge from Leetcode - Pour Water

Показать описание
Problem: You are given an elevation map represented as an integer array heights where heights[i] representing 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 the index k and rests on top of the highest terrain or water at that index. Then, it flows according to the following 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. Also, level means the height of the terrain plus any water in that column.
We can assume there is infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than one grid block, and each unit of water has to be in exactly one block.
Return the resulting elevation map with the water added.
Solution summary:
— Loop ‘volume’ number of times
— Search left of the drop index for the lowest height lower than the drop point height, and add water to it (increase height)
— If no lower height than the drop point height on the left, do the same search on the right - if found - add water to it
— if not lower height on the right or the left, add water to the drop point itself
— return the mutated heights list
Let me know if you have any questions or see ways I could have optimized!
Let's connect!
Get my coding gear / desk setup 👨🏾💻