Skip to content

Commit 34067e4

Browse files
committed
add 769 java
1 parent 31160e6 commit 34067e4

File tree

6 files changed

+222
-5
lines changed

6 files changed

+222
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@
275275
747 | [Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | [Python](./leetcode_python/Array/largest-number-at-least-twice-of-others.py) | _O(n)_ | _O(1)_ | Easy |`good basic`, `data structure`| OK*
276276
755 | [Pour Water](https://leetcode.com/problems/pour-water/) | [Python](./leetcode_python/Array/pour-water.py) | _O(v * n)_ | _O(1)_ | Medium |`complex`| AGAIN (not start)
277277
766 | [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/) | [Python](./leetcode_python/Array/toeplitz-matrix.py) | _O(m * n)_ | _O(1)_ | Easy |`basic`, `matrix`, `google`| AGAIN* (2)
278-
769 | [Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | [Python](./leetcode_python/Array/max-chunks-to-make-sorted.py) | _O(n)_ | _O(1)_ | Medium || AGAIN** (not start)
278+
769 | [Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | [Python](./leetcode_python/Array/max-chunks-to-make-sorted.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MaxChunksToMakeSorted.java)
279+
| _O(n)_ | _O(1)_ | Medium |stack, array, google| AGAIN*** (1)
279280
792 | [Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/) | [Python](./leetcode_python/Array/number-of-matching-subsequences.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/NumberOfMatchingSubsequences.java) | _O(n + w)_ | _O(1)_ | Medium |basic, hash map, google| AGAIN**** (2)
280281
794 | [Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/) | [Python](./leetcode_python/Array/valid-tic-tac-toe-state.py) | _O(1)_ | _O(1)_ | Medium |`complex`| AGAIN
281282
795 | [Number of Subarrays with Bounded Maximum](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/) | [Python](./leetcode_python/Array/number-of-subarrays-with-bounded-maximum.py) | _O(n)_ | _O(1)_ | Medium || AGAIN (not start*)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-12-31
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-12-29
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241231: 769
12
20241229: 498(again),934
23
20241228: 379,173,855(todo)
34
20241227: 079,212(todo),362,849

data/to_review.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1+
2025-02-24 -> ['769']
12
2025-02-22 -> ['498(again),934']
23
2025-02-21 -> ['379,173,855(todo)']
34
2025-02-20 -> ['079,212(todo),362,849']
45
2025-02-15 -> ['369,311']
56
2025-02-14 -> ['370']
67
2025-02-13 -> ['815,871,593,1109']
78
2025-02-07 -> ['560,523']
9+
2025-02-03 -> ['769']
810
2025-02-01 -> ['498(again),934', '304,853,325']
911
2025-01-31 -> ['379,173,855(todo)']
1012
2025-01-30 -> ['079,212(todo),362,849']
1113
2025-01-26 -> ['370(todo)']
1214
2025-01-25 -> ['369,311']
1315
2025-01-24 -> ['370', '34,767']
1416
2025-01-23 -> ['815,871,593,1109']
17+
2025-01-21 -> ['769']
1518
2025-01-20 -> ['722,380']
1619
2025-01-19 -> ['498(again),934', '33,81']
1720
2025-01-18 -> ['379,173,855(todo)']
1821
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
1922
2025-01-16 -> ['776,31']
2023
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2124
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
25+
2025-01-13 -> ['769']
2226
2025-01-12 -> ['369,311']
2327
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
2428
2025-01-10 -> ['379,173,855(todo)', '815,871,593,1109', '833,950']
2529
2025-01-09 -> ['079,212(todo),362,849']
30+
2025-01-08 -> ['769']
2631
2025-01-06 -> ['498(again),934']
27-
2025-01-05 -> ['379,173,855(todo)', '370(todo)']
32+
2025-01-05 -> ['769', '379,173,855(todo)', '370(todo)']
2833
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
29-
2025-01-03 -> ['498(again),934', '370', '34,767', '444']
30-
2025-01-02 -> ['379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
31-
2025-01-01 -> ['498(again),934', '079,212(todo),362,849']
34+
2025-01-03 -> ['769', '498(again),934', '370', '34,767', '444']
35+
2025-01-02 -> ['769', '379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
36+
2025-01-01 -> ['769', '498(again),934', '079,212(todo),362,849']
3237
2024-12-31 -> ['498(again),934', '379,173,855(todo)']
3338
2024-12-30 -> ['498(again),934', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
3439
2024-12-29 -> ['379,173,855(todo)', '079,212(todo),362,849', '370', '304,853,325', '33,81']
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/max-chunks-to-make-sorted/description/
4+
// https://leetcode.cn/problems/max-chunks-to-make-sorted/description/
5+
6+
import java.util.Stack;
7+
8+
/**
9+
* 769. Max Chunks To Make Sorted
10+
* Medium
11+
* Topics
12+
* Companies
13+
* Hint
14+
* You are given an integer array arr of length n that represents a permutation of the integers in the range [0, n - 1].
15+
*
16+
* We split arr into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.
17+
*
18+
* Return the largest number of chunks we can make to sort the array.
19+
*
20+
*
21+
*
22+
* Example 1:
23+
*
24+
* Input: arr = [4,3,2,1,0]
25+
* Output: 1
26+
* Explanation:
27+
* Splitting into two or more chunks will not return the required result.
28+
* For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.
29+
* Example 2:
30+
*
31+
* Input: arr = [1,0,2,3,4]
32+
* Output: 4
33+
* Explanation:
34+
* We can split into two chunks, such as [1, 0], [2, 3, 4].
35+
* However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* n == arr.length
41+
* 1 <= n <= 10
42+
* 0 <= arr[i] < n
43+
* All the elements of arr are unique.
44+
*
45+
*/
46+
public class MaxChunksToMakeSorted {
47+
48+
// V0
49+
// public int maxChunksToSorted(int[] arr) {
50+
//
51+
// }
52+
53+
// V1-1
54+
// https://leetcode.com/problems/max-chunks-to-make-sorted/editorial/
55+
// IDEA: PrefixMax and SuffixMin Arrays
56+
public int maxChunksToSorted_1_1(int[] arr) {
57+
int n = arr.length;
58+
int[] prefixMax = arr.clone();
59+
int[] suffixMin = arr.clone();
60+
61+
// Fill the prefixMax array
62+
for (int i = 1; i < n; i++) {
63+
prefixMax[i] = Math.max(prefixMax[i - 1], prefixMax[i]);
64+
}
65+
66+
// Fill the suffixMin array in reverse order
67+
for (int i = n - 2; i >= 0; i--) {
68+
suffixMin[i] = Math.min(suffixMin[i + 1], suffixMin[i]);
69+
}
70+
71+
int chunks = 0;
72+
for (int i = 0; i < n; i++) {
73+
// A new chunk can be created
74+
if (i == 0 || suffixMin[i] > prefixMax[i - 1]) {
75+
chunks++;
76+
}
77+
}
78+
79+
return chunks;
80+
}
81+
82+
// V1-2
83+
// https://leetcode.com/problems/max-chunks-to-make-sorted/editorial/
84+
// IDEA: Prefix Sums
85+
public int maxChunksToSorted_1_2(int[] arr) {
86+
int n = arr.length;
87+
int chunks = 0, prefixSum = 0, sortedPrefixSum = 0;
88+
89+
// Iterate over the array
90+
for (int i = 0; i < n; i++) {
91+
// Update prefix sum of `arr`
92+
prefixSum += arr[i];
93+
// Update prefix sum of the sorted array
94+
sortedPrefixSum += i;
95+
96+
// If the two sums are equal, the two prefixes contain the same elements; a chunk can be formed
97+
if (prefixSum == sortedPrefixSum) {
98+
chunks++;
99+
}
100+
}
101+
return chunks;
102+
}
103+
104+
// V1-3
105+
// https://leetcode.com/problems/max-chunks-to-make-sorted/editorial/
106+
// IDEA: Monotonic Increasing Stack
107+
public int maxChunksToSorted(int[] arr) {
108+
int n = arr.length;
109+
// Stack to store the maximum elements of each chunk
110+
Stack<Integer> monotonicStack = new Stack<>();
111+
112+
for (int i = 0; i < n; i++) {
113+
// Case 1: Current element is larger, starts a new chunk
114+
if (monotonicStack.isEmpty() || arr[i] > monotonicStack.peek()) {
115+
monotonicStack.push(arr[i]);
116+
} else {
117+
// Case 2: Merge chunks
118+
int maxElement = monotonicStack.peek();
119+
while (!monotonicStack.isEmpty() && arr[i] < monotonicStack.peek()) {
120+
monotonicStack.pop();
121+
}
122+
monotonicStack.push(maxElement);
123+
}
124+
}
125+
return monotonicStack.size();
126+
}
127+
128+
// V2
129+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,4 +1131,82 @@ private List<List<Integer>> getIsland(int[][] grid, int x, int y, List<List<Inte
11311131
return cur; // ?
11321132
}
11331133

1134+
// LC 769
1135+
// https://leetcode.com/problems/max-chunks-to-make-sorted/
1136+
// 3.02 pm - 3.20 pm
1137+
/**
1138+
*
1139+
* We split arr into some number of chunks (i.e., partitions),
1140+
* and individually sort each chunk. After concatenating them,
1141+
* the result should equal the sorted array.
1142+
*
1143+
*
1144+
* Return the largest number of chunks we can make to sort the array.
1145+
*
1146+
*/
1147+
/**
1148+
* IDEA 1) 2 POINTERS ?
1149+
*
1150+
*
1151+
* exp 1: arr = [4,3,2,1,0] -> res = 1
1152+
*
1153+
* -> sorted arr = [0,1,2,3,4]
1154+
*
1155+
* -> compare before VS after
1156+
* -> [4,3,2,1,0] VS [0,1,2,3,4]
1157+
*
1158+
* -> [4,3,2,1,0]
1159+
* i j j j j
1160+
*
1161+
*
1162+
* exp 2: arr = [1,0,2,3,4] -> res = 4
1163+
*
1164+
* -> sorted arr = [0,1,2,3,4]
1165+
*
1166+
* -> compare before VS after
1167+
*
1168+
* -> [1,0,2,3,4]
1169+
* i j
1170+
*
1171+
*
1172+
*
1173+
*
1174+
*
1175+
*
1176+
*/
1177+
public int maxChunksToSorted(int[] arr) {
1178+
1179+
// edge
1180+
if (arr.length == 1) {
1181+
return 1;
1182+
}
1183+
1184+
// copy
1185+
int[] arr_before = arr; // ??
1186+
Arrays.sort(arr);
1187+
1188+
// 2 pointers
1189+
int res = 1;
1190+
List<Integer> tmp = new ArrayList<>();
1191+
for (int i = 0; i < arr.length-1; i++){
1192+
for(int j = i+1; j < arr_before.length; j++){
1193+
tmp.add(arr[j]);
1194+
// copy
1195+
List<Integer> tmp_copy = tmp; // ???
1196+
// sort
1197+
tmp_copy.sort(Integer::compareTo);
1198+
// if(tmp_copy == tmp_copy){
1199+
//
1200+
// }
1201+
}
1202+
}
1203+
1204+
String x = "abc";
1205+
String[] x_ = x.split("");
1206+
1207+
1208+
1209+
return res;
1210+
}
1211+
11341212
}

0 commit comments

Comments
 (0)