860柠檬水找零
class Solution {
public:
bool lemonadeChange(vector<int>& bills)
{
int five = 0, ten = 0;
for (int bill : bills)
{
if(bill==5) five++;
if(bill == 10)
{
if (five <= 0) return false;
ten++;
five--;
}
if(bill==20)
{
if(five>0&&ten>0)
{
five--;
ten--;
}
else if (five >= 3)
{
five -= 3;
}else return false;
}
}
return true;
}
};
406根据身高重建队列
class Solution {
public:
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
sort(people.begin(),people.end(),[](auto a,auto b){
if(a[0]==b[0]) return a[1]<b[1];
return a[0]>b[0];
});
list<vector<int>> que;
for(int i=0;i<people.size();i++)
{
int position = people[i][1];
std::list<vector<int>>::iterator it = que.begin();
while (position--)
{
it++;
}
que.insert(it, people[i]);
}
return vector<vector<int>>(que.begin(), que.end());
}
};
452用最少的箭引爆气球
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points)
{
if (points.size() == 0) return 0;
sort(points.begin(), points.end(), [](auto &a,auto &b){
return a[0]<b[0];
});
int result=1;
for (int i = 1; i < points.size(); i++)
{
if (points[i][0] > points[i - 1][1])
{
result++;
}
else
{
points[i][1] = min(points[i - 1][1], points[i][1]);
}
}
return result;
}
};