|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author Openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/grumpy-bookstore-owner "Grumpy Bookstore Owner") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/distant-barcodes "Distant Barcodes") |
| 11 | + |
| 12 | +## 1053. Previous Permutation With One Swap (Medium) |
| 13 | + |
| 14 | +<p>Given an array <code>A</code> of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than <code>A</code>, that can be <strong>made with one swap</strong> (A <em>swap</em> exchanges the positions of two numbers <code>A[i]</code> and <code>A[j]</code>). If it cannot be done, then return the same array.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | + |
| 18 | +<p><strong>Example 1:</strong></p> |
| 19 | + |
| 20 | +<pre> |
| 21 | +<strong>Input: </strong>[3,2,1] |
| 22 | +<strong>Output: </strong>[3,1,2] |
| 23 | +<strong>Explanation: </strong> |
| 24 | +Swapping 2 and 1. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong>Example 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input: </strong>[1,1,5] |
| 31 | +<strong>Output: </strong>[1,1,5] |
| 32 | +<strong>Explanation: </strong> |
| 33 | +This is already the smallest permutation. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>Example 3:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<strong>Input: </strong>[1,9,4,6,7] |
| 40 | +<strong>Output: </strong>[1,7,4,6,9] |
| 41 | +<strong>Explanation: </strong> |
| 42 | +Swapping 9 and 7. |
| 43 | +</pre> |
| 44 | + |
| 45 | +<p><strong>Example 4:</strong></p> |
| 46 | + |
| 47 | +<pre> |
| 48 | +<strong>Input: </strong>[3,1,1,3] |
| 49 | +<strong>Output: </strong>[1,1,3,3] |
| 50 | +</pre> |
| 51 | + |
| 52 | +<p> </p> |
| 53 | + |
| 54 | +<p><strong>Note:</strong></p> |
| 55 | + |
| 56 | +<ol> |
| 57 | + <li><code>1 <= A.length <= 10000</code></li> |
| 58 | + <li><code>1 <= A[i] <= 10000</code></li> |
| 59 | +</ol> |
0 commit comments