860.柠檬水找零
题目链接:860. 柠檬水找零 - 力扣(LeetCode)
思路:
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0;
int ten = 0;
for(int i = 0; i < bills.length; i++){
if(bills[i] == 5){
five++;
}else if(bills[i] == 10){
ten++;
five--;
}else{
if(ten > 0){
ten--;
five--;
}else{
five -= 3;
}
}
if(ten < 0 || five < 0){
return false;
}
}
return true;
}
}
406.根据身高重建队列
题目链接:406. 根据身高重建队列 - 力扣(LeetCode)
思路:难点在于二维数组的排序
class Solution {
public int[][] reconstructQueue(int[][] people) {
Arrays.sort(people,(a,b) ->{
if(a[0] == b[0]) return a[1] - b[1]; //升序
return b[0] - a[0];//降序
});
LinkedList<int[]> que = new LinkedList<>();
for(int[] p : people){
que.add(p[1],p);
}
return que.toArray(new int[people.length][]);
}
}
452. 用最少数量的箭引爆气球
题目链接:452. 用最少数量的箭引爆气球 - 力扣(LeetCode)
思路:先按最左边界排序,比较下一个左值和最小右边界的大小,小则更新最小由边界,大则最小有边界变成下一个值的最小右边界。
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points,(a, b) -> Integer.compare(a[0], b[0]));
int count = 1;
for(int i = 1; i < points.length; i++){
if(points[i][0] <= points[i-1][1]){ //可以一箭射穿
//更新最小右边界
points[i][1] = Math.min(points[i][1], points[i-1][1]);
}else{
count++;//最小右边界不变
}
}
return count;
}
}