670. Maximum Swap

2 pass

T: O(n)
S: O(n)
class Solution {
    private record Max(int idx, int val){};
    public int maximumSwap(int num) {
        String s = String.valueOf(num);
        int n = s.length();
        Max[] max = new Max[n];
        max[n-1] = new Max(n-1, s.charAt(n-1) - '0');
        for (int i = s.length()-2 ; i >= 0; i--) {
            if (max[i+1].val >= (s.charAt(i) - '0')) {
                max[i] = new Max(max[i+1].idx, max[i+1].val);
            } else {
                max[i] = new Max(i, (s.charAt(i) - '0'));
            }
        }

        int swapIdx1 = -1;
        int swapIdx2 = -1;
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) - '0' < max[i].val) {
                swapIdx1 = i;
                swapIdx2 = max[i].idx;
                break;
            }
        }
        if (swapIdx1 == -1) {
            return num;
        }
        char[] c = s.toCharArray();
        char temp = c[swapIdx1];
        c[swapIdx1] = c[swapIdx2];
        c[swapIdx2] = temp;
        return Integer.parseInt(String.valueOf(c));
    }
}
/**
initial thought:
we want to put a max to the  small one from left
but how to find the max? and if it's 1999, we want the right most 9
so we can scan from right, to find the max idx 
and start from left to find a small number which is smaller than this max, and swap it!


2736
7766

6435
6555

1199
9999


1919
9999

2 pass -> build max array from right (idx, val)

2736
7766 -> max array

then compare from left, the first one num[i] < max[i] -> is the swap point

because in this case: 1199
we want to swap the right most max for the max value! so from right to build max array

also replace from left, so we can have the max number

T: O(n)
S: O(n)
 */

1 pass

Last updated

Was this helpful?