Flipping Burnt Pancakes, but Optimally!
This is based on the Burnt Pancake problem.
In the burnt pancake problem, each “pancake” has a burnt side. You must sort these pancakes in order with the burnt side down. You may only use one tool, your spatula, which can flip the pancakes from the top of the pancake stack to where you inserted the spatula.
Flipping pancakes that have the burnt side down results in those pancakes being in reverse order and having the burnt side up, and vice versa.
For a given arraypancake stack, determinereturn the minimal number of flips needed to be made burnt pancake sorting.
The output must show every step of the optimal flipping process, with the position of the spatula being represented by a pipe character, and u or b after every number representing whether or not a pancake is burnt or unburnt.
Note that this is an NP-HARD problem. You may not make an approximation algorithm.
Testcases
[1,2,3,4,5]1b2b3b4u returns 0,the following:
[3,2,1,4,5] returns1b|2b3b4u
(probably1u2b|3b4u
10,2u|1b3b4u
need2b1b|3b4u
to1u2u3b|4u
verify)3u|2b1b4u
[9,3,1] returns3b2b1b|4u
(probably1u2u3u4u
4b3b2b1b 6,returns needthe tofollowing:
verify)4b3b2b1b|
1u2u3u4u
This is fastest-algorithm, so the minimal time complexity wins.