Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
The problem asks to change the array nums to make it sorted (i.e., all the 1s are on the left of 2s, and all the 2s are on the left of 3s.).
We can try all the possibilities to make nums indices range in [0, i) to 0 and [i, j) to 1 and [j, n) to 2. Note the ranges are left-close and right-open; each might be empty. Namely, 0 <= i <= j <= n.
Count the changes we need for each possibility by comparing the expected and original values at each index position.