Easy
Given an array nums
, you can perform the following operation any number of times:
nums
. If multiple such pairs exist, choose the leftmost one.Return the minimum number of operations needed to make the array non-decreasing.
An array is said to be non-decreasing if each element is greater than or equal to its previous element (if it exists).
Example 1:
Input: nums = [5,2,3,1]
Output: 2
Explanation:
(3,1)
has the minimum sum of 4. After replacement, nums = [5,2,4]
.(2,4)
has the minimum sum of 6. After replacement, nums = [5,6]
.The array nums
became non-decreasing in two operations.
Example 2:
Input: nums = [1,2,2]
Output: 0
Explanation:
The array nums
is already sorted.
Constraints:
1 <= nums.length <= 50
-1000 <= nums[i] <= 1000
public class Solution {
public int minimumPairRemoval(int[] nums) {
int operations = 0;
while (!isNonDecreasing(nums)) {
int minSum = Integer.MAX_VALUE;
int index = 0;
// Find the leftmost pair with minimum sum
for (int i = 0; i < nums.length - 1; i++) {
int sum = nums[i] + nums[i + 1];
if (sum < minSum) {
minSum = sum;
index = i;
}
}
// Merge the pair at index
int[] newNums = new int[nums.length - 1];
int j = 0;
int i = 0;
while (i < nums.length) {
if (i == index) {
newNums[j++] = nums[i] + nums[i + 1];
// Skip the next one since it's merged
i++;
} else {
newNums[j++] = nums[i];
}
i++;
}
nums = newNums;
operations++;
}
return operations;
}
private boolean isNonDecreasing(int[] nums) {
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
return false;
}
}
return true;
}
}