class Solution {
public int minimumOperations(int[] nums) {
Queue<Integer> pq = new PriorityQueue<>();
for (int num : nums) {
pq.offer(num);
}
int ans = 0;
while (!pq.isEmpty()) {
int min = pq.poll();
if (min != 0) {
List<Integer> list = new ArrayList<>();
while (!pq.isEmpty()) {
list.add(pq.poll() - min);
}
for (int num : list) {
pq.offer(num);
}
ans++;
}
}
return ans;
}
}
方法二:哈希表
class Solution {
public int minimumOperations(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (num != 0) {
set.add(num);
}
}
return set.size();
}
}