Skip to content

Commit a27f28a

Browse files
add 769
1 parent 23e6b29 commit a27f28a

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Your ideas/fixes/algorithms are more than welcome!
3333
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | O(logn) | O(1) | |Medium|
3434
|776|[Split BST](https://leetcode.com/problems/split-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_776.java) | O(n) | O(n) | |Medium| Recursion
3535
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | O(n) | O(m) | |Easy|
36+
|769|[Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_769.java) | O(n) | O(1) | |Medium| Array
3637
|767|[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_767.java) | O(klogk) k is the number of unique characters in given String| O(k) | |Medium|
3738
|766|[Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_766.java) | O(m*n) | O(1) | |Easy|
3839
|765|[Couples Holding Hands](https://leetcode.com/problems/couples-holding-hands/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_765.java) | O(n^2) | O(1) | |Hard|
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
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+
*/
33+
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++;
55+
}
56+
}
57+
return result + 1;
58+
}
59+
}
60+
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, max = 0;
65+
for (int i = 0; i < arr.length; ++i) {
66+
max = Math.max(max, arr[i]);
67+
if (max == i) ans++;
68+
}
69+
return ans;
70+
}
71+
}
72+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._769;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _769Test {
10+
private static _769.Solution1 solution1;
11+
private static _769.Solution2 solution2;
12+
private static int[] arr;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _769.Solution1();
17+
solution2 = new _769.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
arr = new int[] {4, 3, 2, 1, 0};
23+
assertEquals(1, solution1.maxChunksToSorted(arr));
24+
assertEquals(1, solution2.maxChunksToSorted(arr));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
arr = new int[] {1, 0, 2, 3, 4};
30+
assertEquals(4, solution1.maxChunksToSorted(arr));
31+
assertEquals(4, solution2.maxChunksToSorted(arr));
32+
}
33+
}

0 commit comments

Comments
 (0)