Skip to content

Commit 2528a9b

Browse files
Create max_chunks_to_make_sorted.cpp
1 parent 689e1c9 commit 2528a9b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

max_chunks_to_make_sorted.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxChunksToSorted(vector<int>& arr) {
4+
5+
int max_ = 0, result = 0;
6+
//we find the max_seen_so_far
7+
//and if max_seen_so_far == current_index, we can divide the array in one chunk.
8+
9+
for(int i=0; i<arr.size(); i++) {
10+
max_ = max(max_, arr[i]);
11+
if(max_ == i) result++;
12+
}
13+
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)