代码随想录day29 134. 加油站 、135. 分发糖果 、 860.柠檬水找零 、406.根据身高重建队列
代码随想录
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int curSum = 0;
int totalSum = 0;
int start = 0;
for (int i = 0; i < gas.size(); i++) {
curSum += gas[i] - cost[i];
totalSum += gas[i] - cost[i];
if (curSum < 0) {
start = i + 1;
curSum = 0;
}
}
return totalSum < 0 ? -1 : start;
}
};
代码随想录
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int changes[2] = {0};
for (auto money : bills) {
switch(money) {
case 5:
changes[0]++;
break;
case 10:
if (--changes[0] < 0) {
return false;
}
changes[1]++;
break;
case 20:
if (changes[1] > 0 && changes[0] > 0) {
changes[1]--;
changes[0]--;
break;
}
if (changes[0] > 2) {
changes[0] -= 3;
break;
}
return false;
}
}
return true;
}
};
代码随想录
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int changes[2] = {0};
for (auto money : bills) {
switch(money) {
case 5:
changes[0]++;
break;
case 10:
if (--changes[0] < 0) {
return false;
}
changes[1]++;
break;
case 20:
if (changes[1] > 0 && changes[0] > 0) {
changes[1]--;
changes[0]--;
break;
}
if (changes[0] > 2) {
changes[0] -= 3;
break;
}
return false;
}
}
return true;
}
};
代码随想录
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(),people.end(),[](const vector<int> &lhs,const vector<int> &rhs) {
if (lhs[0] != rhs[0]) {
return lhs[0] > rhs[0];
}
return lhs[1] < rhs[1];
});
vector<vector<int>> res;
for (int i = 0; i < people.size(); i++) {
res.insert(res.begin() + people[i][1], people[i]);
}
return res;
}
};