lundi 24 octobre 2016

Stacks -- UVa 514 - Rails

Problem Link- http://ift.tt/2ezq2Xo

I know how to solve this problem but I am not clear why this method is correct. I can't understand why the following is the approach to this problem. Please help me understand.

It can be solved by

"Keep a stack of integers that represents the station, and a list of integers that has all the trains in order on side A. Iterate through the input. For each train, if it's on top of the stack, then pop it off, and if it's somewhere in the list, the push all the trains in front of it onto the stack, then discard the train. If the train isn't in either location, then it must be buried in the stack and is therefore unreachable, so the arrangement can't be made."

Aucun commentaire:

Enregistrer un commentaire