Leetcode 31: Next permutation

Here are my whiteboard notes for solving this:

So it turns out this algorithm was first discovered a long time ago. According to Knuth (in The Art Of Computer Programming 7.2.1.2), it goes back to the 18th century. This is a nice writeup of the algorithm:
https://www.nayuki.io/page/next-lexicographical-permutation-algorithm

Here is my c++ source code for the solution:

This entry was posted in programming and tagged , , . Bookmark the permalink.

1 Response to Leetcode 31: Next permutation

  1. Pingback: Leetcode 46: Permutations | adamk.org

Leave a Reply

Your email address will not be published. Required fields are marked *