Help us understand the problem. What is going on with this article?

31. Next Permutation

I did't know what next permutation really is but I have found some explanations on Wikipedia, and it helped.

Quick Explanation

The explanation below is grab from next permutation section on Wikipedia.

cf. https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order

  1. Find the largest index k such that a[k] < a[k+1]. If no such index exist, the permutation is the last permutation.
  2. Find the largest index l greater than k such that a[k] < a[l].
  3. Swap the value of a[k] with that of a[l].
  4. Reverse the sequence from a[k+1] up to and including th final element a[n].

Other Resources

Code

class Solution {
    func nextPermutation(_ nums: inout [Int]) {
        // I have created methods for each step
        let k = findLargestK(&nums) // ①
        if k >= 0 {
            let l = findLargestL(&nums, k) // ②
            swap(&nums, k, l) // ③
        }
        reverse(&nums, k + 1) // ④
    }

    /// Find the largest index k such that a[k] < a[k+1].
    /// If no such index exist, the permutation is the last permutation.
    private func findLargestK(_ nums: inout [Int]) -> Int {
        // We start from the back because we need to find a largest index.
        // -2 for we need an extra space for k+1
        var k = nums.count - 2

        while k >= 0 && nums[k] >= nums[k+1] {
            k -= 1
        }

        return k
    }

    /// Find the largest index l greater than k such that a[k] < a[l].
    private func findLargestL(_ nums: inout [Int], _ k: Int) -> Int {
        // We start from the back because we need to find a largest index.
        var l = nums.count - 1

        while l >= 0 && nums[k] >= nums[l] {
            l -= 1
        }

        return l
    }

    private func swap(_ nums: inout [Int], _ a: Int, _ b: Int) {
        let temp = nums[a]
        nums[a] = nums[b]
        nums[b] = temp
    }

    private func reverse(_ nums: inout [Int], _ start: Int) {
        var lower = start
        var upper = nums.count - 1

        while lower < upper {
            swap(&nums, lower, upper)
            lower += 1
            upper -= 1
        }
    }
}
vc7
iOS Developer from 🇹🇼
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした