-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
36 lines (30 loc) · 947 Bytes
/
solution.java
File metadata and controls
36 lines (30 loc) · 947 Bytes
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
class Solution {
public int minimumPairRemoval(int[] nums) {
int operations = 0;
List<Integer> list = new ArrayList<>();
for (int num : nums)
list.add(num);
while (!isSorted(list)) {
int minSum = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < list.size() - 1; i++) {
int sum = list.get(i) + list.get(i + 1);
if (sum < minSum) {
minSum = sum;
index = i;
}
}
list.set(index, minSum);
list.remove(index + 1);
operations++;
}
return operations;
}
private boolean isSorted(List<Integer> list) {
for (int i = 1; i < list.size(); i++) {
if (list.get(i) < list.get(i - 1))
return false;
}
return true;
}
}