860. 柠檬水找零
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
vector<int> lingqian(2, 0); //下标0 : 5块, 1: 10块
int remain = 0; //当前你身上有多少
int need;
for (int i = 0; i < bills.size(); i++) {
need = bills[i] - 5;
//当需要找回的钱 > 你身上有的钱
if (need > remain) return false;
//顾客支付5块
if (bills[i] == 5) {
lingqian[0] += 1;
//顾客支付10块
}else if (bills[i] == 10) {
if (lingqian[0] == 0) return false;
lingqian[0] -= 1;
lingqian[1] += 1;
//顾客支付20块
}else {
if(lingqian[0] >= 1 && lingqian[1] >= 1) {
lingqian[0] -= 1;
lingqian[1] -= 1;
}else if(lingqian[0] >= 3) {
lingqian[0] -= 3;
}else {
return false;
}
}
remain += 5;
}
return true;
}
};
406. 根据身高重建队列
class Solution {
static bool cmp(vector<int>& a, vector<int>& b) {
//当升高相同,按照前面的人数从小到大排列
if (a[0] == b[0]) return a[1] < b[1];
//按照频次排序
return a[0] > b[0];
}
public:
//遇到多维度的队列,可以考虑先处理一个维度再处理另外一个维度
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> queue;
sort(people.begin(), people.end(), cmp);
int position;//元素待插入的位置
for (int i = 0; i < people.size(); i++) {
position = people[i][1];
queue.insert(queue.begin() + position, people[i]);
}
return queue;
}
};
452. 用最少数量的箭引爆气球
class Solution {
private:
//左边界从小到大排序
static bool cmp(vector<int>& a, vector<int>& b) {
return a[0] < b[0];
}
public:
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(), points.end(), cmp);
//当没有气球的时候, 0支弓箭
if (points.size() == 0) return 0;
//至少一只弓箭
int jian = 1;
for (int i = 1; i < points.size(); i++) {
//上一个区域的右边界小于当前区域的左边界
if (points[i][0] > points[i-1][1]) {
jian += 1;
}else {
points[i][1] = min(points[i-1][1], points[i][1]);
}
}
return jian;
}
};