Skip to content

Commit 990596a

Browse files
committed
Time: 0 ms (100.00%), Space: 40.9 MB (62.41%) - LeetHub
1 parent 546eec9 commit 990596a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)