-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumPairRemoval.java
More file actions
44 lines (38 loc) · 1.25 KB
/
Copy pathMinimumPairRemoval.java
File metadata and controls
44 lines (38 loc) · 1.25 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
import java.util.*;
class Solution {
public int minimumPairRemoval(int[] nums) {
int operations = 0;
// Convert to a List for easy manipulation
List<Integer> list = new ArrayList<>();
for (int num : nums) {
list.add(num);
}
// Continue until the array is non-decreasing
while (!isNonDecreasing(list)) {
int minSum = Integer.MAX_VALUE;
int leftIndex = -1;
// Find the leftmost pair with the minimum sum
for (int i = 0; i < list.size() - 1; i++) {
int currentSum = list.get(i) + list.get(i + 1);
if (currentSum < minSum) {
minSum = currentSum;
leftIndex = i;
}
}
// Replace the pair with their sum
list.set(leftIndex, minSum);
list.remove(leftIndex + 1);
operations++;
}
return operations;
}
// Helper method to check if the list is non-decreasing
private boolean isNonDecreasing(List<Integer> list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) > list.get(i + 1)) {
return false;
}
}
return true;
}
}