Description Solution: Stack Note: The positions don't affect which robots will collide with each other, this stays the same no matter what. Robots adjacent to each other going right and left will always collide. In the stack, keep track of robots we have processed so far that have still survived. If the current robot is going left, We need to eliminate all right-going robots with smaller health at the top of the stack, while decreasing the health of the current robot. If the robot at the top of the stack is right-going and the health is greater, then remove the current robot and decrease the health of the robot at the top of the stack. If the robot at the top of the stack is right-going and the health is the same, remove both. If the current robot is going right, then just push it onto the stack to be dealt with later. n = number of robots Time Complexity: O(n log(n)) Space Complexity: O(n) var survivedRobotsHealths = function(positions, healths, directions) { let n...
Comments
Post a Comment