Skip to content

Commit 0834acd

Browse files
refactor 769
1 parent e02999c commit 0834acd

File tree

1 file changed

+40
-66
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+40
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,49 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 769. Max Chunks To Make Sorted
5-
6-
Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into some number of "chunks" (partitions), and individually sort each chunk.
7-
After concatenating them, the result equals the sorted array.
8-
9-
What is the most number of chunks we could have made?
10-
11-
Example 1:
12-
13-
Input: arr = [4,3,2,1,0]
14-
Output: 1
15-
Explanation:
16-
Splitting into two or more chunks will not return the required result.
17-
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.
18-
19-
Example 2:
20-
21-
Input: arr = [1,0,2,3,4]
22-
Output: 4
23-
Explanation:
24-
We can split into two chunks, such as [1, 0], [2, 3, 4].
25-
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.
26-
27-
Note:
28-
29-
arr will have length in range [1, 10].
30-
arr[i] will be a permutation of [0, 1, ..., arr.length - 1].
31-
32-
*/
333
public class _769 {
34-
public static class Solution1 {
35-
/**credit: https://leetcode.com/problems/max-chunks-to-make-sorted/discuss/113520/Java-solution-left-max-and-right-min.*/
36-
public int maxChunksToSorted(int[] arr) {
37-
int len = arr.length;
38-
39-
int[] maxOfLeft = new int[len];
40-
maxOfLeft[0] = arr[0];
41-
for (int i = 1; i < len; i++) {
42-
maxOfLeft[i] = Math.max(arr[i], maxOfLeft[i - 1]);
43-
}
44-
45-
int[] minOfRight = new int[len];
46-
minOfRight[len - 1] = arr[len - 1];
47-
for (int i = len - 2; i >= 0; i--) {
48-
minOfRight[i] = Math.min(minOfRight[i + 1], arr[i]);
49-
}
50-
51-
int result = 0;
52-
for (int i = 0; i < len - 1; i++) {
53-
if (maxOfLeft[i] <= minOfRight[i + 1]) {
54-
result++;
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/max-chunks-to-make-sorted/discuss/113520/Java-solution-left-max-and-right-min.
7+
*/
8+
public int maxChunksToSorted(int[] arr) {
9+
int len = arr.length;
10+
11+
int[] maxOfLeft = new int[len];
12+
maxOfLeft[0] = arr[0];
13+
for (int i = 1; i < len; i++) {
14+
maxOfLeft[i] = Math.max(arr[i], maxOfLeft[i - 1]);
15+
}
16+
17+
int[] minOfRight = new int[len];
18+
minOfRight[len - 1] = arr[len - 1];
19+
for (int i = len - 2; i >= 0; i--) {
20+
minOfRight[i] = Math.min(minOfRight[i + 1], arr[i]);
21+
}
22+
23+
int result = 0;
24+
for (int i = 0; i < len - 1; i++) {
25+
if (maxOfLeft[i] <= minOfRight[i + 1]) {
26+
result++;
27+
}
28+
}
29+
return result + 1;
5530
}
56-
}
57-
return result + 1;
5831
}
59-
}
6032

61-
public static class Solution2 {
62-
/**credit: https://leetcode.com/articles/max-chunks-to-make-sorted-i/*/
63-
public int maxChunksToSorted(int[] arr) {
64-
int ans = 0;
65-
int max = 0;
66-
for (int i = 0; i < arr.length; ++i) {
67-
max = Math.max(max, arr[i]);
68-
if (max == i) {
69-
ans++;
33+
public static class Solution2 {
34+
/**
35+
* credit: https://leetcode.com/articles/max-chunks-to-make-sorted-i/
36+
*/
37+
public int maxChunksToSorted(int[] arr) {
38+
int ans = 0;
39+
int max = 0;
40+
for (int i = 0; i < arr.length; ++i) {
41+
max = Math.max(max, arr[i]);
42+
if (max == i) {
43+
ans++;
44+
}
45+
}
46+
return ans;
7047
}
71-
}
72-
return ans;
7348
}
74-
}
7549
}

0 commit comments

Comments
 (0)