第一题没啥问题,第二题感觉没啥问题但是就过了45,有点离谱,代码贴来,大佬们帮忙看一下
public static void solution2(item[] items,int w,int k){
Arrays.sort(items, new Comparator<item>() { @Override public int compare(item o1, item o2) {
if(o2.heart!=o1.heart)return o2.heart-o1.heart;
else{
if(o1.price!=o2.price)
return o1.price-o2.price;
else return o1.weight-o2.weight;
}
}
});
int sump = 0,sumw = 0;
int res = 0;
for(item i:items){
if(sump+i.price<=k && sumw+i.weight<=w){
sump+=i.price;
sumw+=i.weight;
res++;
}else break;
}
System.out.println(res);
}
static class item{
int weight;
int price;
int heart;
item(int weight,int price,int heart){
this.weight=weight;
this.price=price;
this.heart=heart;
}
}
全部评论
(0) 回帖