This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Solution to CodeSignal arcade level "stringsRearrangement" (https://app.codesignal.com/arcade/intro/level-7/PTWhv2oWqd6p4AHB9) using a custom next-permutation-function without recursion. | |
* It switches the values in an integer-array, so I can use the values in this array as my index while looping through the strings-array (it simulates reordering the strings themselves). | |
* | |
* Permutation logic: | |
* 1) find index of last increasing value in array of ints and save position before in variable i | 0, 2, 4, 3, 1 -> index of last increasing value = [2]:4 -> i = [1]:2 | |
* 2) replace value at i with the nearest increasing number after it | [1]:2 <-> [3]:3 -> 0, 3, ... | |
* 3) at the same time take remaining numbers after i, sort them and fill the remaining spots with them in ascending order | -> 0, 3, 1, 2, 4 | |
*/ |