package leetcode;

/**
 * https://leetcode.com/problems/monotone-increasing-digits/
 */
public class Problem738 {
    public int monotoneIncreasingDigits(int N) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem738 prob = new Problem738();
        System.out.println(prob.monotoneIncreasingDigits(10)); // 9
        System.out.println(prob.monotoneIncreasingDigits(1234)); // 1234
        System.out.println(prob.monotoneIncreasingDigits(332)); // 299
        System.out.println(prob.monotoneIncreasingDigits(1357)); // 1357
        System.out.println(prob.monotoneIncreasingDigits(9753)); // 8999
        System.out.println(prob.monotoneIncreasingDigits(9735)); // 8999
        System.out.println(prob.monotoneIncreasingDigits(999)); // 999
        System.out.println(prob.monotoneIncreasingDigits(555)); // 555
        System.out.println(prob.monotoneIncreasingDigits(8123)); // 7999
        System.out.println(prob.monotoneIncreasingDigits(123765)); // 123699
    }
}

@@ -168,852 +168,1313 @@
     
-// TODO%0A        return 0
+String str = Integer.toString(N);%0A        StringBuilder sb = new StringBuilder()
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem738 prob = new Problem738();%0A        System.out.println(prob.monotoneIncreasingDigits(10)); // 9%0A        System.out.println(prob.monotoneIncreasingDigits(1234)); // 1234%0A        System.out.println(prob.monotoneIncreasingDigits(332)); // 299%0A        System.out.println(prob.monotoneIncreasingDigits(1357)); // 1357%0A        System.out.println(prob.monotoneIncreasingDigits(9753)); // 8999%0A        System.out.println(prob.monotoneIncreasingDigits(9735)); // 8999%0A        System.out.println(prob.monotoneIncreasingDigits(999)); // 999%0A        System.out.println(prob.monotoneIncreasingDigits(555)); // 555%0A        System.out.println(prob.monotoneIncreasingDigits(8123)); // 7999%0A        System.out.println(prob.monotoneIncreasingDigits(123765)); // 123699
+boolean nine = false;%0A        int count = 0;%0A        for (int i = 0; i %3C str.length(); i++) %7B%0A            if (nine) %7B%0A                sb.append('9');%0A                continue;%0A            %7D%0A            if (i + 1 %3C str.length()) %7B%0A                if (str.charAt(i) %3C str.charAt(i + 1)) %7B%0A                    for (int j = 0; j %3C count; j++) %7B%0A                        sb.append(str.charAt(i));%0A                    %7D%0A                    sb.append(str.charAt(i));%0A                    count = 0;%0A                %7D else if (str.charAt(i) %3E str.charAt(i + 1)) %7B%0A                    if (str.charAt(i) - 1 != '0') %7B%0A                        sb.append((char) (str.charAt(i) - 1));%0A                    %7D%0A                    for (int j = 0; j %3C count; j++) %7B%0A                        sb.append('9');%0A                    %7D%0A                    nine = true;%0A                    count = 0;%0A                %7D else %7B%0A                    count++;%0A                %7D%0A            %7D%0A        %7D%0A        if (!nine) %7B%0A            for (int j = 0; j %3C count; j++) %7B%0A                sb.append(str.charAt(str.length() - 1));%0A            %7D%0A            sb.append(str.charAt(str.length() - 1));%0A        %7D%0A        return Integer.parseInt(sb.toString());
 %0A   

Update problem 738