Problem editorialmedium
The next permutation
Reordering a sequence into the very next one in dictionary order. Not by listing all N! arrangements — by making the smallest possible change as far right as it can go.
20 min read·time O(N)·space O(1)
Greedy three-step walkRightmost changeReverse the suffix