Official algorithmic problem description and constraints.
Let's define a cyclic shift of some string π as a transformation from π 1π 2β¦π πβ1π π into π ππ 1π 2β¦π πβ1. In other words, you take one last character π π and place it before the first character while moving all other characters to the right.
You are given a binary string π (a string consisting of only 0-s and/or 1-s).
In one operation, you can choose any substring π ππ π+1β¦π π (1β€π<πβ€|π |) and cyclically shift it. The cost of such operation is equal to πβπ+1 (or the length of the chosen substring).
You can perform the given operation any number of times. What is the minimum total cost to make π sorted in non-descending order?
Detailed video explanations, mathematical intuition, and clean C++ implementation code.