package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/max-chunks-to-make-sorted/
 */
public class Problem769 {
    public int maxChunksToSorted(int[] arr) {
        int[] sorted = Arrays.copyOf(arr, arr.length);
        Arrays.sort(sorted);
        int result = 0;
        int i = 0;
        int j = i;
        while (i < sorted.length) {
            while (j < arr.length && arr[j] != sorted[i]) {
                j++;
            }
            i = j;
            result++;
        }
        return result;
    }

    public static void main(String[] args) {
        Problem769 prob = new Problem769();
//        System.out.println(prob.maxChunksToSorted(new int[]{4, 3, 2, 1, 0})); // 1
//        System.out.println(prob.maxChunksToSorted(new int[]{1, 0, 2, 3, 4})); // 4
//        System.out.println(prob.maxChunksToSorted(new int[]{1, 0, 1, 2, 3, 4})); // 5
        System.out.println(prob.maxChunksToSorted(new int[]{2, 0, 1})); // 1
        System.out.println(prob.maxChunksToSorted(new int[]{2, 0, 1, 5})); // 2
//        System.out.println(prob.maxChunksToSorted(new int[]{1, 2})); // 2
//        System.out.println(prob.maxChunksToSorted(new int[]{2, 1})); // 1
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -167,79 +167,38 @@
  int
-%5B%5D sorted = Arrays.copyOf(arr, arr.length);%0A        Arrays.sort(sorted)
+ result = 0;%0A        int j = 0
 ;%0A  
@@ -203,34 +203,32 @@
         int 
-result = 0
+max = -1
 ;%0A        in
@@ -221,24 +221,29 @@
 -1;%0A        
+for (
 int i = 0;%0A 
@@ -244,177 +244,324 @@
 = 0;
-%0A        int j = i;%0A        while (i %3C sorted.length) %7B%0A            while (j %3C arr.length && arr%5Bj%5D != sorted%5Bi%5D) %7B%0A                j++;%0A            %7D%0A            i = j;
+ i %3C arr.length; i++) %7B%0A            if (i %3C= max) %7B%0A                continue;%0A            %7D%0A            while (j %3C arr.length && arr%5Bj%5D != i) %7B%0A                max = Math.max(max, arr%5Bj%5D);%0A                j++;%0A            %7D%0A            if (j %3C arr.length) %7B%0A                max = Math.max(max, arr%5Bj%5D);%0A            %7D
 %0A   
@@ -947,18 +947,15 @@
 %7B1, 
-0, 1
+3
 , 2, 
-3
+0
 , 4%7D
@@ -965,10 +965,12 @@
  // 
-5%0A
+2%0A//
     
@@ -1038,24 +1038,26 @@
  1%7D)); // 1%0A
+//
         Syst
@@ -1117,9 +1117,9 @@
  1, 
-5
+3
 %7D));
@@ -1276,16 +1276,111 @@
 ); // 1%0A
+        System.out.println(prob.maxChunksToSorted(new int%5B%5D%7B1, 4, 3, 6, 0, 7, 8, 2, 5%7D)); // 1%0A
     %7D%0A%7D%0A

Update problem 769