Doubt in Increasing-Decreasing (Cook off)-(Beginner)

EDIT: I AM ABLE TO SOLVE THIS FOR FULL MARKS NOW BUT HAVEN’T STILL UNDERSTOOD HOW SET USAGE INSTEAD OF A LIST IMPROVED IT SO DRASTICALLY.Can someone help me with the links or explanations

I read the editorial and saw a few solutions. My approach is basic and similar to some people. It should have got atleast 50. But it doesn’t pass the first case too. Can someone help me out in this by looking at my solution: https://www.codechef.com/viewsolution/34837697

Also refer this solution because this is quite similar but has passed all cases: https://www.codechef.com/viewsolution/34832410

Input:

1
6
1 2 3 2 3 4

Your code outputs

YES
1 2 3 4 2 3

expected output is

YES
1 2 3 4 3 2

Also, look at sets in python, for better time complexity.

I read about sets and I just changed that second list to set and got 100. I am amazed. I still couldn’t figure out the how it improved the complexity such drastically…

Please help me my code is https://www.codechef.com/viewsolution/34839368
please tell me where I am wrong.
Thanks