Intuition
We use a greedy approach to find minimum key pushes - since the keys are in a circular manner, there are two distances between any two keys. We always prefer the shortest distance.
To visualize it, think of a circle with a chord through it that divides the circle into two arcs - one arc is going to be larger than the other. If the both arcs are equal, that means our chord is a diameter that divides the circle into two equal halves.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since we would be visiting all characters in the string of length N.
-
Memory
The memory usage is since we are not using any extra data structure.
— A