1.分发饼干
1.先排序
2.遍历饼干的尺寸,找到合适的胃口时,表示胃口的指针加一
class Solution {
public int findContentChildren(int[] g, int[] s) {
int sum = 0;
int start = 0;//孩子胃口遍历的指针
//按照从小到大顺序排序
Arrays.sort(g);
Arrays.sort(s);
/**
遍历饼干的尺寸,找到适合的胃口,遍历胃口的指针加一,sum++
*/
for(int i=0;i<s.length&&start<g.length;i++){
if(s[i]>=g[start]){
sum++;
start++;
}
}
return sum;
}
}
2. 柠檬水找零
1.定义5元钱的数量,定义10元钱的数量
2.当顾客支付20元时,优先支付10元(如果有10元钞票)+5元,或者支付3个5元
class Solution {
public boolean lemonadeChange(int[] bills) {
int cash_5 = 0;//5元钞票
int cash_10 = 0;//10元钞票
for(int i = 0;i<bills.length;i++){
//收到5元钞票
if(bills[i]==5){
cash_5++;
}
//收到10元钞票
else if(bills[i]==10){
cash_5--;
cash_10++;
}
//收到20元钞票,优先找10+5
else if(bills[i]==20){
if(cash_10>0){
cash_10--;
cash_5--;
}else{
cash_5-=3;
}
}
if(cash_5<0||cash_10<0) return false;
}
return true;
}
}