【发布时间】:2020-09-10 10:52:18
【问题描述】:
我如何摆脱这条消息?
bad operand types for binary operator '-'
Queue<Long> heap = new PriorityQueue( (a,b)->b - a );
first type: Object
second type: Object
import java.util.*;
class solve {
static long minCost(long arr[], int n) {
Queue<Long> heap = new PriorityQueue( (a,b)-> b - a );
long ans = 0;
for( long i : arr )
heap.add(i);
while(!heap.isEmpty()){
long f = heap.poll();
if( heap.isEmpty()) return f+ans;
long s = heap.poll();
ans += f+s;
heap.add(f+s);
}
return ans;
}
public static void main(String[] args) {
long a[] = {4, 3, 2, 6};
System.out.println(minCost(a, 4));
}
}
【问题讨论】:
-
为什么不
Comparator.reverseOrder()? -
首先:
PriorityQueue->PriorityQueue<>然后修复下一个错误
标签: java collections queue priority-queue