Skip to content

Commit 778c550

Browse files
committed
update 769 java
1 parent 4eadd7e commit 778c550

File tree

2 files changed

+121
-27
lines changed

2 files changed

+121
-27
lines changed

leetcode_java/src/main/java/LeetCodeJava/Array/MaxChunksToMakeSorted.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,42 @@
4646
public class MaxChunksToMakeSorted {
4747

4848
// V0
49-
// public int maxChunksToSorted(int[] arr) {
50-
//
51-
// }
49+
// IDEA: prefix + problem understanding
50+
/**
51+
* IDEA:
52+
*
53+
* since this problem wants to find the MAX value of sub array with sort op
54+
* and concatenate can same as the SORTED ORIGINAL ARRAY
55+
*
56+
* -> so all we need to do within looping is:
57+
* maintain
58+
* 1) sorted prefixSum (sort all array and its prefix sum)
59+
* 2) prefixSum (current array's prefix sum)
60+
*
61+
* -> so, for `sorted prefixSum`, it's sortedPrefixSum + i, ... (since the array is the permutation of element within [0, n - 1])
62+
* for `prefixSum`, it's prefixSum + i ,....
63+
*
64+
*/
65+
public int maxChunksToSorted(int[] arr) {
66+
67+
int n = arr.length;
68+
int chunks = 0, prefixSum = 0, sortedPrefixSum = 0;
69+
70+
// Iterate over the array
71+
for (int i = 0; i < n; i++) {
72+
// Update prefix sum of `arr`
73+
prefixSum += arr[i];
74+
// Update prefix sum of the sorted array
75+
sortedPrefixSum += i;
76+
77+
// If the two sums are equal, the two prefixes contain the same elements; a
78+
// chunk can be formed
79+
if (prefixSum == sortedPrefixSum) {
80+
chunks++;
81+
}
82+
}
83+
return chunks;
84+
}
5285

5386
// V1-1
5487
// https://leetcode.com/problems/max-chunks-to-make-sorted/editorial/
@@ -163,7 +196,7 @@ public int maxChunksToSorted_1_2(int[] arr) {
163196
// V1-3
164197
// https://leetcode.com/problems/max-chunks-to-make-sorted/editorial/
165198
// IDEA: Monotonic Increasing Stack
166-
public int maxChunksToSorted(int[] arr) {
199+
public int maxChunksToSorted_1_3(int[] arr) {
167200
int n = arr.length;
168201
// Stack to store the maximum elements of each chunk
169202
Stack<Integer> monotonicStack = new Stack<>();

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

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ private List<List<Integer>> getIsland(int[][] grid, int x, int y, List<List<Inte
11651165

11661166
// LC 769
11671167
// https://leetcode.com/problems/max-chunks-to-make-sorted/
1168-
// 3.02 pm - 3.20 pm
1168+
// 1.24 pm - 1.35 pm
11691169
/**
11701170
*
11711171
* We split arr into some number of chunks (i.e., partitions),
@@ -1206,40 +1206,101 @@ private List<List<Integer>> getIsland(int[][] grid, int x, int y, List<List<Inte
12061206
*
12071207
*
12081208
*/
1209+
/**
1210+
* IDEA V2: pre-fix sum
1211+
*
1212+
* -> to check if the array can be split into sub array and sorted,
1213+
* then finally can concatenate and as same as the original array
1214+
*
1215+
* Exp 1: arr = [4,3,2,1,0] -> res = 1
1216+
*
1217+
* -> sorted arr = [0,1,2,3,4]
1218+
*
1219+
* -> arrList = [4,3,2,1,0]
1220+
* -> preFixSum = [4,7,9,10,10]
1221+
*
1222+
*
1223+
*
1224+
* Exp 2:
1225+
*
1226+
* arr = [1,0,2,3,4] -> res = 4
1227+
*
1228+
* -> sorted arr = [0,1,2,3,4]
1229+
*
1230+
* [1,0,2,3,4]
1231+
*
1232+
* -> arrList = [1,0,2,3,4]
1233+
* -> preFixSum = [1,0,3,6,10]
1234+
*
1235+
*
1236+
*
1237+
*
1238+
*/
12091239
public int maxChunksToSorted(int[] arr) {
12101240

12111241
// edge
12121242
if (arr.length == 1) {
12131243
return 1;
12141244
}
12151245

1216-
// copy
1217-
int[] arr_before = arr; // ??
1218-
Arrays.sort(arr);
1219-
1220-
// 2 pointers
1221-
int res = 1;
1222-
List<Integer> tmp = new ArrayList<>();
1223-
for (int i = 0; i < arr.length-1; i++){
1224-
for(int j = i+1; j < arr_before.length; j++){
1225-
tmp.add(arr[j]);
1226-
// copy
1227-
List<Integer> tmp_copy = tmp; // ???
1228-
// sort
1229-
tmp_copy.sort(Integer::compareTo);
1230-
// if(tmp_copy == tmp_copy){
1231-
//
1232-
// }
1246+
List<Integer> preFixSortedSum = new ArrayList<>();
1247+
List<Integer> arrList = new ArrayList<>(); // ??
1248+
1249+
int preSortedSum = 0;
1250+
int preSum = 0;
1251+
1252+
// prefix sum
1253+
int cnt = 0;
1254+
for(int i = 0; i < arr.length; i++){
1255+
preSum += i;
1256+
preSortedSum += arr[i];
1257+
1258+
if(preSum != preSortedSum){
1259+
cnt += 1;
1260+
}else{
1261+
arrList.add(arr[i]);
1262+
preFixSortedSum.add(preSum);
12331263
}
12341264
}
12351265

1236-
String x = "abc";
1237-
String[] x_ = x.split("");
1238-
1266+
return cnt;
1267+
}
12391268

12401269

1241-
return res;
1242-
}
1270+
// public int maxChunksToSorted(int[] arr) {
1271+
//
1272+
// // edge
1273+
// if (arr.length == 1) {
1274+
// return 1;
1275+
// }
1276+
//
1277+
// // copy
1278+
// int[] arr_before = arr; // ??
1279+
// Arrays.sort(arr);
1280+
//
1281+
// // 2 pointers
1282+
// int res = 1;
1283+
// List<Integer> tmp = new ArrayList<>();
1284+
// for (int i = 0; i < arr.length-1; i++){
1285+
// for(int j = i+1; j < arr_before.length; j++){
1286+
// tmp.add(arr[j]);
1287+
// // copy
1288+
// List<Integer> tmp_copy = tmp; // ???
1289+
// // sort
1290+
// tmp_copy.sort(Integer::compareTo);
1291+
//// if(tmp_copy == tmp_copy){
1292+
////
1293+
//// }
1294+
// }
1295+
// }
1296+
//
1297+
// String x = "abc";
1298+
// String[] x_ = x.split("");
1299+
//
1300+
//
1301+
//
1302+
// return res;
1303+
// }
12431304

12441305
// LC 817
12451306
// https://leetcode.com/problems/linked-list-components/

0 commit comments

Comments
 (0)