1053. Previous Permutation With One Swap Medium
1/**
2 * [1053] Previous Permutation With One Swap
3 *
4 * Given an array of positive integers arr (not necessarily distinct), return the lexicographically largest permutation that is smaller than arr, that can be made with exactly one swap (A swap exchanges the positions of two numbers arr[i] and arr[j]). If it cannot be done, then return the same array.
5 *
6 * Example 1:
7 *
8 * Input: arr = [3,2,1]
9 * Output: [3,1,2]
10 * Explanation: Swapping 2 and 1.
11 *
12 * Example 2:
13 *
14 * Input: arr = [1,1,5]
15 * Output: [1,1,5]
16 * Explanation: This is already the smallest permutation.
17 *
18 * Example 3:
19 *
20 * Input: arr = [1,9,4,6,7]
21 * Output: [1,7,4,6,9]
22 * Explanation: Swapping 9 and 7.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= arr.length <= 10^4
28 * 1 <= arr[i] <= 10^4
29 *
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/previous-permutation-with-one-swap/
34// discuss: https://leetcode.com/problems/previous-permutation-with-one-swap/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39 pub fn prev_perm_opt1(arr: Vec<i32>) -> Vec<i32> {
40 vec![]
41 }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_1053() {
52 }
53}
54
Back
© 2025 bowen.ge All Rights Reserved.