加油站
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int sum = 0;
int min = 0;
for (int i = 0; i < gas.length; i++) {
sum += (gas[i] - cost[i]);
min = Math.min(sum, min);
}
if (sum < 0) return -1;
if (min >= 0) return 0;
for (int i = gas.length - 1; i > 0; i--) {
min += (gas[i] - cost[i]);
if (min >= 0) return i;
}
return -1;
}
}
思路就是计算每个店的结余,但是实现起来如果是暴力的话会超市
柠檬水
int five=0;
int ten=0;
for(int i=0;i<bills.length;i++){
if (bills[i]==5) {
five+=1;
}else if(bills[i]==10){
if (five<1) {
return false;
}
five-=1;
ten+=1;
}else if(bills[i]==20){
if (ten>0&&five>0) {
five-=1;
ten-=1;
}else if (ten==0&&five<3) {
return false;
}else{
five-=3;
}
}
}
return true;
理清楚20时候的逻辑