Editorial for Encryption by Reversal
Submitting an official solution before solving the problem yourself is a bannable offence.
Authors:
Time complexity: .
To solve this, we can use dynamic programming.
Let us consider a dp array , assuming we are 1-indexed.
Therefore, we know the solution at will be , because we do not have to flip any integer to find are solution only for the range . This also means that the solution at will be because at , this signifies we have flipped the integer at index .
Now that we have clarified the state, let us move onto the transition.
We run into 4 cases, where we store 4 variables.
If the current integer is greater than the previous, then this means as we do not flip any integers.
If the current integer is greater than the previous reversed integer, then this means .
If the current integer reversed is greater than the previous, then this means .
If the current integer reversed is greater than the previous reversed integer, then this means .
Therefore our solution, after running this for , is .
Comments