LeetCode 1053. Previous Permutation With One Swap

原题链接在这里:https://leetcode.com/problems/previous-permutation-with-one-swap/

题目:

Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.

Example 1:

Input: [3,2,1]
Output: [3,1,2]
Explanation: Swapping 2 and 1.

Example 2:

Input: [1,1,5]
Output: [1,1,5]
Explanation: This is already the smallest permutation.

Example 3:

Input: [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: Swapping 9 and 7.

Example 4:

Input: [3,1,1,3]
Output: [1,3,1,3]
Explanation: Swapping 1 and 3.

Note:

  1. 1 <= A.length <= 10000
  2. 1 <= A[i] <= 10000

题解:

From right to left, find the first element A[i - 1] > A[i]. Mark first position = i - 1.

If we can‘t find it, then there is no previouis perrmutation.

Then mark second position = i, and for j > i, find the last position that A[j] > A[j - 1] && A[j] < A[first]. If we find any, keep updating second position.

Swap first and second positions.

Time Complexity: O(n). n = A.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] prevPermOpt1(int[] A) {
 3         if(A == null || A.length == 0){
 4             return A;
 5         }
 6
 7         int n = A.length;
 8         int i = n - 1;
 9         while(i >= 1 && A[i] >= A[i - 1]){
10             i--;
11         }
12
13         if(i == 0){
14             return A;
15         }
16
17         int first = i - 1;
18         int second = i;
19         for(int j = i + 1; j < n; j++){
20             if(A[j] > A[j - 1] && A[j] < A[first]){
21                 second = j;
22             }
23         }
24
25         int temp = A[first];
26         A[first] = A[second];
27         A[second] = temp;
28         return A;
29     }
30 }

类似Next Permutation.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12151385.html

时间: 2024-11-11 12:52:30

LeetCode 1053. Previous Permutation With One Swap的相关文章

leetcode_1053. Previous Permutation With One Swap

1053. Previous Permutation With One Swap https://leetcode.com/problems/previous-permutation-with-one-swap/ 题意:Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, t

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

Next Permutation &amp; Previous Permutation

Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the next permutation is [1,3,3,2] For [4,3,2,1], the next per

[array] leetcode - 31. Next Permutation - Medium

leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

LeetCode 31. Next Permutation

Problem: https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest poss

LeetCode 31. Next Permutation (下一个排列)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

[LeetCode][JavaScript]Next Permutation

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde

LeetCode 31 Next Permutation(下一个排列)

翻译 实现"下一个排列"函数,将排列中的数字重新排列成字典序中的下一个更大的排列. 如果这样的重新排列是不可能的,它必须重新排列为可能的最低顺序(即升序排序). 重排必须在原地,不分配额外的内存. 以下是一些示例,左侧是输入,右侧是输出: 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 原文 Implement next permutation, which rearranges numbers into the lexicographically