Gamified challenge · Stacks
plates is a tower of plates listed bottom first: plates[0] is at the bottom and the last item is on top. Each number is a plate's width. You may lift only the top plate of a tower, you have one spare tower and one free hand, and you must never bury a plate you still need to move.
Write sortStack(plates) (Python: sort_stack(plates)) that returns the tidy tower, again bottom first: the widest plate at the bottom and the narrowest on top, so the list is in non-increasing order. Equal plates simply sit together.
For [3, 1, 4, 2] the answer is [4, 3, 2, 1]. Calling a library sort gets the same list, but the point is to do it with the spare tower: lift a plate, move narrower plates back off the spare, and set it down.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google