We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 546eec9 commit 990596aCopy full SHA for 990596a
0769-max-chunks-to-make-sorted/0769-max-chunks-to-make-sorted.java
@@ -0,0 +1,24 @@
1
+class Solution {
2
+ public int maxChunksToSorted(int[] arr) {
3
+ int n = arr.length;
4
+ int[] maxOfLeft = new int[n];
5
+ int[] minOfRight = new int[n];
6
+
7
+ maxOfLeft[0] = arr[0];
8
+ for (int i = 1; i < n; i++) {
9
+ maxOfLeft[i] = Math.max(maxOfLeft[i-1], arr[i]);
10
+ }
11
12
+ minOfRight[n - 1] = arr[n - 1];
13
+ for (int i = n - 2; i >= 0; i--) {
14
+ minOfRight[i] = Math.min(minOfRight[i + 1], arr[i]);
15
16
17
+ int res = 0;
18
+ for (int i = 0; i < n - 1; i++) {
19
+ if (maxOfLeft[i] <= minOfRight[i + 1]) res++;
20
21
22
+ return res + 1;
23
24
+}
0 commit comments