본문 바로가기
LeetCode

[LeetCode] 31. Next Permutation

by JungSeung 2023. 10. 31.
728x90

https://leetcode.com/

permutation of an array of integers is an arrangement of its members into a sequence or linear order.

  • For example, for arr = [1,2,3], the following are all the permutations of arr[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. 

More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.

Given an array of integers nums, find the next permutation of nums.

The replacement must be in place and use only constant extra memory.


Example 1:

nput: nums = [1,2,3]
Output: [1,3,2]

 

Example 2:

Input: nums = [3,2,1]
Output: [1,2,3]

 

Example 3:

Input: nums = [1,1,5]
Output: [1,5,1]

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

Code :

var nextPermutation = function(nums) {
  const n = nums.length;
    
    // Step 1: 비감소 접미사의 끝을 찾습니다.
    let i = n - 2;
    while (i >= 0 && nums[i] >= nums[i + 1]) {
        i--;
    }
    
    // Step 2: 이러한 요소가 없는 경우 전체 배열이 내림차순이므로 배열을 뒤집습니다.
    if (i === -1) {
        nums.reverse();
        return;
    }
    
    // Step 3: nums[i]보다 큰 접미사에서 가장 작은 요소를 찾습니다.
    let j = n - 1;
    while (nums[j] <= nums[i]) {
        j--;
    }
    
    // Step 4: nums[i]와 nums[j]를 교환합니다.
    [nums[i], nums[j]] = [nums[j], nums[i]];
    
    // Step 5: 접미사를 뒤집습니다.
    let left = i + 1;
    let right = n - 1;
    while (left < right) {
        [nums[left], nums[right]] = [nums[right], nums[left]];
        left++;
        right--;
    }
};

 

Solutions Code :

var nextPermutation = function(nums) {
    let n = nums.length;
    let k, l;

    // 비감소 접미사의 끝을 찾습니다.
    for (k = n - 2; k >= 0; k--) {
        if (nums[k] < nums[k + 1]) {
            break;
        }
    }

    // 비감소 접미사의 끝이 없는 경우 배열을 뒤집습니다.
    if (k < 0) {
        nums.reverse();
    } else {
        // 접미사의 다음 큰 숫자를 찾습니다.
        for (l = n - 1; l > k; l--) {
            if (nums[l] > nums[k]) {
                break;
            }
        }
        // k와 l을 교환합니다.
        [nums[k], nums[l]] = [nums[l], nums[k]];
        // k 이후의 숫자들을 뒤집습니다.
        nums.splice(k + 1, n - k - 1, ...nums.slice(k + 1).reverse());
    }
}

 

 

출처 : https://leetcode.com/problemset/all/

 

Problems - LeetCode

Boost your coding interview skills and confidence by practicing real interview questions with LeetCode. Our platform offers a range of essential problems for practice, as well as the latest questions being asked by top-tier companies.

leetcode.com

 

728x90