My solution combines those two approaches.
- Calculate average value dynamicly, if the first node is a is below average, push to b, otherwise, rotate to the bottom. Repeat till 5 nodes are left in stack a.
- Using linked list, each node in b will have the "target" in a, "target" is the node in a which contains the smallest bigger number than the node in stack b. Thus, when we push b to a, the smaller value will be on top of the bigger one.
Reference: Explains the logic of pushing below averages, and finding the "best friend": https://github.com/duarte3333/Push_Swap
Explains double linkedlist and find the "cheapest nodes": https://github.com/suspectedoceano/push_swap/tree/main