赞
踩
贪心思路:
class Solution { public boolean lemonadeChange(int[] bills) { int[] moneyCount = new int[3]; for(int i = 0 ; i < bills.length;i++){ if(bills[i]==5){ moneyCount[0]++; }else if (bills[i]==10){ moneyCount[1]++; moneyCount[0]--; }else if(bills[i]==20){ if(moneyCount[1] > 0){ moneyCount[1]--; moneyCount[0]--; }else{ moneyCount[0]-=3; } } if(moneyCount[0]<0 || moneyCount[1]<0){ return false; } } return true; } }
贪心思路:
p[1]
进行插入class Solution { public int[][] reconstructQueue(int[][] people) { Arrays.sort(people,(o1,o2)->{ if(o1[0]==o2[0]) return o1[1]-o2[1]; return o2[0]-o1[0]; } ); List<int[]> res = new LinkedList<>(); for(int[] n:people){ res.add(n[1],n); } return res.toArray(new int[res.size()][]); } }
贪心思路:
遍历区间:
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points,(o1,o2) -> Integer.compare(o1[0],o2[0]));
int res = 1;
for(int i = 1 ; i < points.length;i++){
if(points[i-1][1] < points[i][0]){
res++;
}else{
points[i][1] = Math.min(points[i-1][1],points[i][1]);
}
}
return res;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。