-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3507.cpp
More file actions
39 lines (39 loc) · 1.03 KB
/
3507.cpp
File metadata and controls
39 lines (39 loc) · 1.03 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
class Solution {
public:
bool isSorted(vector<int>& nums) {
int minVal = INT_MIN;
for (auto& num : nums) {
if (num < minVal) return false;
minVal = num;
}
return true;
}
int minimumPairRemoval(vector<int>& nums) {
int count = 0;
while (!isSorted(nums)) {
count++;
vector<int> temp;
int index = 0;
int currSum = INT_MAX;
int n = nums.size();
for (int i = 0; i < n - 1; ++i) {
int sum = nums[i] + nums[i + 1];
if (sum < currSum) {
currSum = sum;
index = i;
}
}
for (int i = 0; i < n; ++i) {
if (i == index) {
temp.push_back(nums[i] + nums[i + 1]);
i++;
}
else {
temp.push_back(nums[i]);
}
}
nums = temp;
}
return count;
}
};