We can just simulate the process mentioned in the description by maintaining a variable for the direction.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we are simulating the process for each second till (T-1).
-
Memory
The memory usage is since no explicit data-structure is used in place.
— A