Trains the technique from
LeetCode 31Next PermutationThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A display shelf carries one numbered price plate per slot, given left to right as the integer array plates. Head office ranks every possible left-to-right reading of this exact multiset of plates, comparing two readings position by position and putting the one with the smaller number at the first position where they differ ahead of the other.
Every morning the shelf has to move to whichever reading sits immediately after the current reading in that ranking. If the current reading is already the very last one, the shelf wraps around to the first reading in the ranking, which is the plates arranged in non-decreasing order.
Rearrange plates itself. You may hold a fixed number of extra variables and nothing that grows with the number of slots, so no second array and no list of candidate readings. Return plates after rearranging it.
Example 1
Ranked, the readings of these three plates run 2 4 5, then 2 5 4, then 4 2 5, and so on. The shelf currently shows the second of those, so it moves to the third.
Example 2
No reading of these plates ranks above 7 6 3, so the shelf wraps back to the lowest ranked reading.
Example 3
The 1 in slot 0 is the last plate that has a larger plate somewhere to its right. Trading it with the smallest such plate, the 2, and then laying the remaining tail out in non-decreasing order gives the next reading up.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def next_arrangement(plates: list[int]) -> list[int]:public int[] nextArrangement(int[] plates)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.