The TransCompilator

Solved LeetCode program · 814 of 1,000

#1053 · Previous Permutation With One Swap

Medium Array chunk-17.md

Original English source

Keep a shared numbers holding 1, 9, 4, 6, and 7. Keep a shared pivot of -1. Keep a shared bestspot of 0. Keep a shared bestvalue of 0.

To choose.
For every spot from pivot plus 1 to the last position of numbers.
The value is item spot of numbers. The place is pivot. The cap is item place of numbers. If value is less than cap and value is at least bestvalue, set bestvalue to value, set bestspot to spot.

The last is the last position of numbers.
For every step from 0 to last minus 1.
The spot is last minus 1 minus step. If item spot of numbers is more than item (spot plus 1) of numbers, set pivot to spot, stop.

Set bestspot to pivot plus 1. The target is bestspot. If pivot is at least 0, set bestvalue to item target of numbers. If pivot is at least 0, Choose. The place is pivot. The other is bestspot. If pivot is at least 0, swap item place of numbers and item other of numbers. For each value in numbers, Show value.

Open in Playground Browse all 1,000 problems

Expected stdout

1
7
4
6
9