Simple Java Solution with explanation
In Ver 1, the basic idea is to use max[] array to keep track of the max value until the current position, and compare it to the sorted array. If the max[i] equa
In Ver 1, the basic idea is to use max[] array to keep track of the max value until the current position, and compare it to the sorted array. If the max[i] equals the element at index i in the sorted array, then the final count++.\nHere the only difference is that we need to a variable 'upperLimit' to help determine whether the current point is the correct division position.\n\nFor example,\n```\noriginal:     2, 1, 4, 4, 3, 5, 7, 6\nmax:          2, 2, 4, 4, 4, 5, 7, 7\nsorted:       1, 2, 3, 4, 4, 5, 6, 7\n```\nThe chunks are: 2, 1  |  4, 4, 3  |  5  |  7, 6\n\nIt needs to be noted that at index 3, although max[i] == sorted[i], this is not the right dividing point. Otherwise, the number after it (3) will be in the wrong chunk.\n\n```\n    public int maxChunksToSorted(int[] arr) {\n        if (arr == null || arr.length == 0) return 0;\n        \n        int[] sorted = arr.clone();\n        Arrays.sort(sorted);\n        \n        int[] max = new int[arr.length];\n        max[0] = arr[0];\n        for (int i = 1; i < arr.length; i++) {\n            max[i] = Math.max(max[i - 1], arr[i]);\n        }\n        \n        int count = 0;\n        int upperLimit = Integer.MAX_VALUE;\n        for (int i = arr.length - 1; i >= 0; i--) {\n            if (max[i] == sorted[i]) {\n                if (sorted[i] > upperLimit) continue;\n                \n                count++;\n                upperLimit = arr[i];\n            }\n        }\n        \n        return count;\n    }\n```
[]