We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 1f61987 + 2dc4c86 commit c70a9c0Copy full SHA for c70a9c0
3507. Minimum Pair Removal to Sort Array I
@@ -0,0 +1,31 @@
1
+class Solution {
2
+public:
3
+ int minimumPairRemoval(vector<int>& nums) {
4
+ int operations = 0;
5
+
6
+ while (!isNonDecreasing(nums)) {
7
+ int minSum = nums[0] + nums[1];
8
+ int index = 0;
9
10
+ for (int i = 1; i < nums.size() - 1; i++) {
11
+ int sum = nums[i] + nums[i + 1];
12
+ if (sum < minSum) {
13
+ minSum = sum;
14
+ index = i;
15
+ }
16
17
18
+ nums[index] = minSum;
19
+ nums.erase(nums.begin() + index + 1);
20
+ operations++;
21
22
+ return operations;
23
24
25
+ bool isNonDecreasing(vector<int>& nums) {
26
+ for (int i = 1; i < nums.size(); i++) {
27
+ if (nums[i] < nums[i - 1]) return false;
28
29
+ return true;
30
31
+};
0 commit comments