-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_chunks_to_sorted.cpp
More file actions
51 lines (47 loc) · 1.3 KB
/
max_chunks_to_sorted.cpp
File metadata and controls
51 lines (47 loc) · 1.3 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* =====================================================================================
*
* Filename: max_chunks_to_sorted.cpp
*
* Description: 769. Max Chunks To Make Sorted
* https://leetcode.com/problems/max-chunks-to-make-sorted/
*
* Version: 1.0
* Created: 05/18/2025 22:58:22
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <utility>
#include <vector>
#include "gtest/gtest.h"
class Solution {
public:
int maxChunksToSorted(std::vector<int>& arr) {
int index_sum = 0;
int prefix_sum = 0;
int chunks = 0;
for (int i = 0; i < arr.size(); i++) {
index_sum += i;
prefix_sum += arr[i];
if (prefix_sum == index_sum) {
chunks++;
}
}
return chunks;
}
};
TEST(Solution, maxChunksToSorted) {
std::vector<std::pair<std::vector<int>, int>> cases = {
std::make_pair(std::vector<int>{4, 3, 2, 1, 0}, 1),
std::make_pair(std::vector<int>{1, 0, 2, 3, 4}, 4),
std::make_pair(std::vector<int>{3, 1, 5, 7, 4, 6, 2, 0}, 1),
};
for (auto& [arr, chunks] : cases) {
EXPECT_EQ(Solution().maxChunksToSorted(arr), chunks);
}
}