860.柠檬水找零
题目
文章讲解
视频讲解
思路:分别列出三种支付方式对应的找零情况
class Solution {public boolean lemonadeChange(int[] bills) {int five = 0, ten = 0, twenty = 0;for (int i = 0; i < bills.length; i++) {if (bills[i] == 5) {five++;} else if (bills[i] == 10) {if (five < 0)return false;else {five--;ten++;}} else {if (five > 0 && ten > 0) {five--;ten--;twenty++;} else if (five >= 3) {five -= 3;twenty++;} else {return false;}}}return true;}
}
406.根据身高重建队列
题目
文章讲解
视频讲解
思路:分两个维度求解,固定好一个维度后再进行另一个维度的插入。先按照身高降序排序,如果身高相同则按照k值升序排序。然后根据k值将每个人插入到队列的指定位置
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. 用最少数量的箭引爆气球
题目
文章讲解
视频讲解
思路:先按左边界排序,重叠气球最小右边界需要射一箭,当前气球右边界取重叠部分最小值
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]) {count++;} else {points[i][1] = Math.min(points[i][1], points[i - 1][1]);}}return count;}
}