levinoscar / sunsetviewstack Goto Github PK
View Code? Open in Web Editor NEWSolves the sunset view problem by maintaining a running maximum of building heights. Uses a while loop to loop in the direction away from the sun and keep track of the maximum height seen so far. If the current building is taller, it can see the sun, otherwise it can't. So at each iteration, it compares the height of the current building to the running maximum and updates the running maximum accordingly. Runs in O(n) time and O(n) space.