-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL769.java
More file actions
30 lines (30 loc) · 995 Bytes
/
L769.java
File metadata and controls
30 lines (30 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution769 {
class Solution {
/**
* 769. Max Chunks To Make Sorted https://leetcode.com/problems/max-chunks-to-make-sorted/description/
* Since the array contains elements in the range n - 1, we just count the number of boundaries before which max is
* equal to i.
*
* @param arr int[]
* The input array
* @return
* @timeComplexity O(n) Where n is the length of input array
* @spaceComplexity O(1)
*/
public int maxChunksToSorted(int[] arr) {
if (arr.length == 1) {
return 1;
}
int max = Integer.MIN_VALUE;
int chunks = 0;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
if (max == i) {
chunks++;
max = Integer.MIN_VALUE;
}
}
return chunks;
}
}
}