860.柠檬水找零
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
unordered_map<int, int> money = {{5,0}, {10,0}, {20,0}};
for(int i =0; i<bills.size(); i++){
if(bills[i]==5){
money[5]++;
}
if(bills[i] == 10){
money[10]++;
money[5]--;
}
if(bills[i]==20){
money[20]++;
if(money[10]>0){
money[10]--;
money[5]--;
}else{
money[5]-=3;
}
}
if(money[5]<0){
return false;
}
}
return true;
}
};
参考文章:代码随想录-860.柠檬水找零
406.根据身高重建队列
class Solution {
public:
static bool cmp(const vector<int>& a, const vector<int>& b) {
if (a[0] == b[0]) return a[1] < b[1];
return a[0] > b[0];
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(), people.end(), cmp);
vector<vector<int>> re_peo;
for(int i=0; i<people.size(); i++){
int index = people[i][1];
re_peo.insert(re_peo.begin()+index, people[i]);
}
return re_peo;
}
};
参考文章:代码随想录- 406.根据身高重建队列
452. 用最少数量的箭引爆气球
class Solution {
public:
static bool cmp(const vector<int>& a, const vector<int>& b) {
return a[0]<b[0];
}
int findMinArrowShots(vector<vector<int>>& points) {
if (points.size() == 0) return 0;
sort(points.begin(), points.end(), cmp);
int sum = 1;
for(int i=1; i<points.size(); i++){
if(points[i][0]>points[i-1][1]){
sum++;
}else{
points[i][1] = min(points[i-1][1], points[i][1]);
}
}
return sum;
}
};