目录
LeetCode 860.柠檬水找零
文章讲解:代码随想录
视频讲解:贪心算法,看上去复杂,其实逻辑都是固定的!LeetCode:860.柠檬水找零_哔哩哔哩_bilibili
力扣题目:LeetCode 860.柠檬水找零
代码如下(Java):
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){
five--;
ten++;
}else if(bills[i] == 20){
if(ten > 0){
ten--;
five--;
}else{
five -= 3;
}
}
if(five < 0 || ten < 0) return false;
}
return true;
}
}
LeetCode 406.根据身高重建队列
文章讲解:代码随想录
视频讲解:贪心算法,不要两边一起贪,会顾此失彼 | LeetCode:406.根据身高重建队列_哔哩哔哩_bilibili
代码如下(Java):
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][]);
}
}
LeetCode 452.用最少数量的箭引爆气球
文章讲解:代码随想录
代码如下(Java):
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;
}
}