CodeWars 일백 일곱 번째 문제

Updated:

Next smaller number with the same digits

public static long nextSmaller(long n) {
    char[] s = String.valueOf(n).toCharArray();

    for (int i = s.length - 2; i >= 0; i--) {
        for (int j = s.length - 1; j > i; j--) {

            if (s[i] > s[j]) {
                swap(s, i, j);
                char[] copy = Arrays.copyOfRange(s, i + 1, s.length);

                Arrays.sort(copy);
                reverse(copy);

                System.arraycopy(copy, 0, s, i+1, copy.length);

                long result = Long.valueOf(String.valueOf(s));

                if((int)Math.log10(result) == (int)Math.log10(n)) {
                    return result;
                }

                return -1;
            }
        }
    }

    return -1;
}

static void swap(char[] a, int idx1, int idx2) {
    char t = a[idx1];
    a[idx1] = a[idx2];
    a[idx2] = t;
}

static void reverse(char[] a) {
    for (int i = 0; i < a.length / 2; i++) {
        swap(a, i, a.length - 1-i);
    }
}
  • 몇 일 전에 다음 큰수를 찾는 문제를 못 풀었었다.
  • 하지만 그때 공부한 내용을 바탕으로 이번에는 쉽게 풀 수 있었다.