Day35 贪心 part04
我的思路:
只要逐个考虑bills数组可能的数字 5/10/20,分别考虑
解答:
class Solution {
public boolean lemonadeChange(int[] bills) {
int fivecount = 0;
int tencount = 0;
for(int bill : bills) {
if(bill == 5) {
fivecount++;
}
else if(bill == 10) {
if(fivecount <= 0) {
return false;
}
fivecount --;
tencount ++;
}
else {
if(fivecount > 0 && tencount > 0) {
fivecount --;
tencount --;
}
else if(fivecount >= 3) {
fivecount -= 3;
}
else {
return false;
}
}
}
return true;
}
}
我的思路:
类似分糖果,people[i] = [hi, ki] 如果身高h相同,k按照从小到大排序,否则按照身高h从高到低排序
解答:
class Solution {
public int[][] reconstructQueue(int[][] people) {
ArrayList<int[] > list = new ArrayList<>();
Arrays.sort(people, (a, b) -> a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]);
for(int[] person : people) {
list.add(person[1], person);
}
int[][] res = new int[people.length][2];
for(int i = 0; i < people.length; i ++) {
res[i] = list.get(i);
}
return res;
}
}
我的思路:
需要注意的是,对point进行排序的时候,是按end值进行从小到大排序
解答:
class Solution {
public int findMinArrowShots(int[][] points) {
Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1]));
int arrow = 1;
int end = points[0][1];
for(int i = 0; i < points.length; i ++) {
if(points[i][0] > end) {
arrow ++;
end = points[i][1];
}
}
return arrow;
}
}