You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
281
281
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*)
* An important observation is that a segment of the
322
+
* array can form a valid chunk if, when sorted,
323
+
* it matches the corresponding segment
324
+
* in the fully sorted version of the array.
325
+
*
326
+
* Since the numbers in arr belong to the range [0, n - 1], we can simplify the problem by using the property of sums. Specifically, for any index, it suffices to check whether the sum of the elements in arr up to that index is equal to the sum of the elements in the corresponding prefix of the sorted array.
327
+
*
328
+
* If these sums are equal, it guarantees that the elements in the current segment of arr match the elements in the corresponding segment of the sorted array (possibly in a different order). When this condition is satisfied, we can form a new chunk — either starting from the beginning of the array or the end of the previous chunk.
329
+
*
330
+
* For example, consider arr = [1, 2, 0, 3, 4] and the sorted version sortedArr = [0, 1, 2, 3, 4]. We find the valid segments as follows:
331
+
*
332
+
* Segment [0, 0] is not valid, since sum = 1 and sortedSum = 0.
333
+
* Segment [0, 1] is not valid, since sum = 1 + 2 = 3 and sortedSum = 0 + 1 = 1.
334
+
* Segment [0, 2] is valid, since sum = 1 + 2 + 0 = 3 and sortedSum = 0 + 1 + 2 = 3.
335
+
* Segment [3, 3] is valid, because sum = 1 + 2 + 0 + 3 = 6 and sortedSum = 0 + 1 + 2 + 3 = 6.
336
+
* Segment [4, 4] is valid, because sum = 1 + 2 + 0 + 3 + 4 = 10 and sortedSum = 0 + 1 + 2 + 3 + 4 = 10.
337
+
* Therefore, the answer here is 3.
338
+
*
339
+
* Algorithm
340
+
* - Initialize n to the size of the arr array.
341
+
* - Initialize chunks, prefixSum, and sortedPrefixSum to 0.
342
+
* - Iterate over arr with i from 0 to n - 1:
343
+
* - Increment prefixSum by arr[i].
344
+
* - Increment sortedPrefixSum by i.
345
+
* - Check if prefixSum == sortedPrefixSum:
346
+
* - If so, increment chunks by 1.
347
+
* - Return chunks.
348
+
*
349
+
*/
350
+
publicint maxChunksToSorted_1_1(int[] arr) {
351
+
int n = arr.length;
352
+
int chunks =0, prefixSum =0, sortedPrefixSum =0;
353
+
354
+
// Iterate over the array
355
+
for (int i =0; i < n; i++) {
356
+
// Update prefix sum of `arr`
357
+
prefixSum += arr[i];
358
+
// Update prefix sum of the sorted array
359
+
sortedPrefixSum += i;
360
+
361
+
// If the two sums are equal, the two prefixes contain the same elements; a chunk can be formed
* An important observation is that a segment of the
60
+
* array can form a valid chunk if, when sorted,
61
+
* it matches the corresponding segment
62
+
* in the fully sorted version of the array.
63
+
*
64
+
* Since the numbers in arr belong to the range [0, n - 1], we can simplify the problem by using the property of sums. Specifically, for any index, it suffices to check whether the sum of the elements in arr up to that index is equal to the sum of the elements in the corresponding prefix of the sorted array.
65
+
*
66
+
* If these sums are equal, it guarantees that the elements in the current segment of arr match the elements in the corresponding segment of the sorted array (possibly in a different order). When this condition is satisfied, we can form a new chunk — either starting from the beginning of the array or the end of the previous chunk.
67
+
*
68
+
* For example, consider arr = [1, 2, 0, 3, 4] and the sorted version sortedArr = [0, 1, 2, 3, 4]. We find the valid segments as follows:
69
+
*
70
+
* Segment [0, 0] is not valid, since sum = 1 and sortedSum = 0.
71
+
* Segment [0, 1] is not valid, since sum = 1 + 2 = 3 and sortedSum = 0 + 1 = 1.
72
+
* Segment [0, 2] is valid, since sum = 1 + 2 + 0 = 3 and sortedSum = 0 + 1 + 2 = 3.
73
+
* Segment [3, 3] is valid, because sum = 1 + 2 + 0 + 3 = 6 and sortedSum = 0 + 1 + 2 + 3 = 6.
74
+
* Segment [4, 4] is valid, because sum = 1 + 2 + 0 + 3 + 4 = 10 and sortedSum = 0 + 1 + 2 + 3 + 4 = 10.
75
+
* Therefore, the answer here is 3.
76
+
*
77
+
* Algorithm
78
+
* - Initialize n to the size of the arr array.
79
+
* - Initialize chunks, prefixSum, and sortedPrefixSum to 0.
* Building on the above observation, we further notice that for each number in the array, we have two options: we can either include it in the same chunk as the previous number or create a new chunk for it. However, we must consider the limitation that a new chunk at index i can only be created if all the numbers in the current and previous chunks (the "prefix" of the array) are smaller than all the numbers in the following chunks (the "suffix" of the array). This is equivalent to checking whether:
115
+
*
116
+
* max(prefix[0:i])<min(suffix[i:n]).
117
+
*
118
+
*
119
+
* Since we aim to find the largest possible number of chunks, we will choose the second option (i.e., create a new chunk) whenever the above condition is satisfied. Therefore, the problem reduces to counting how many indices in the array satisfy this condition.
120
+
*
121
+
* Algorithm
122
+
* - Initialize n to the size of the arr array.
123
+
* - Initialize prefixMax and suffixMin arrays to arr.
124
+
* - Iterate over arr with i from 1 to n - 1:
125
+
* - Set prefixMax[i] = max(prefixMax[i], prefixMax[i-1]).
126
+
* - Iterate over arr with i from n - 2 to 0:
127
+
* - Set suffixMin[i] = min(suffixMin[i], suffixMin[i+1]).
128
+
* - Initialize chunks to 0.
129
+
* - Iterate over arr with i from 0 to n - 1:
130
+
* - Check if i == 0 (create a chunk for the first element) or suffixMin[i] > prefixMax[i - 1].
131
+
* - If true, increment chunks by 1.
132
+
* - Return chunks.
133
+
*
134
+
*
135
+
*/
136
+
publicintmaxChunksToSorted_1_2(int[] arr) {
57
137
intn = arr.length;
58
138
int[] prefixMax = arr.clone();
59
139
int[] suffixMin = arr.clone();
@@ -79,27 +159,6 @@ public int maxChunksToSorted_1_1(int[] arr) {
0 commit comments