![]() In this case the next permutation would be the array in ascending order. That is array is already in descending order. What if you are checking from the back and you find no problem child.Well if you look to the right of 4, everything is sorted (). When we found 4, how do we efficiently find the smallest number greater than 4 to it's right. Leetcode Problem 31 (Medium): Next Permutation.We can find the number, then the next step, we will start from right most to leftward, try to find the first number which is larger than 3, in this case it is 4. ![]() So we reverse the whole array, for example, 6,5,4,3,2,1 we turn it to 1,2,3,4,5,6. To get the smallest permutation with 3,5 fixed at the start you sort the remaining elements. This means this permutation is the last permutation, we need to rotate back to the first permutation. Now who will replace 4? It will be the smallest number greater than 4 to the right of 4. is descending, is descending, is also descending, is also descending, ah-a is not descending. So you start checking from the back of the array if it is in descending order or not. Well you know that the last permutation has every number in descending order.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |