1 class Solution { 2 public: 3 bool lemonadeChange(vector<int>& bills) { 4 int five = 0; 5 int ten = 0; 6 int twenty = 0; 7 for(int i = 0; i < bills.size(); i++){ 8 switch(bills[i]){ 9 case 5: 10 five++; 11 break; 12 case 10: 13 if(five > 0){ 14 five--; 15 ten++; 16 }else{ 17 return false; 18 } 19 break; 20 case 20: 21 if(five == 0){ 22 return false; 23 }else{ 24 if(ten > 0){ 25 ten--; 26 five--; 27 twenty++; 28 }else{ 29 if(five >= 3){ 30 five -= 3; 31 twenty++; 32 }else{ 33 return false; 34 } 35 } 36 } 37 break; 38 } 39 } 40 return true; 41 } 42 };
先按照身高排序,身高高且位置小的排前面,然后遍历数组,按位置顺序插入新数组
1 class Solution { 2 public: 3 static bool comp(const vector<int>& a, const vector<int>& b){ 4 if(a[0] == b[0]) return a[1] < b[1]; 5 return a[0] > b[0]; 6 } 7 vector<vector<int>> reconstructQueue(vector<vector<int>>& people) { 8 sort(people.begin(),people.end(),comp); 9 vector<vector<int>> que; 10 for(int i = 0; i < people.size(); i++){ 11 int pos = people[i][1]; 12 que.insert(que.begin() + pos, people[i]); 13 } 14 return que; 15 } 16 };
1 class Solution { 2 public: 3 static bool comp(const vector<int>& a, const vector<int>& b){ 4 return a[0] < b[0]; 5 } 6 int findMinArrowShots(vector<vector<int>>& points) { 7 if(points.size() == 0) return 0; 8 sort(points.begin(),points.end(),comp); 9 int result = 1; 10 for(int i = 1; i < points.size(); i++){ 11 if(points[i][0] > points[i-1][1])// 跟前一个不挨着 12 result++; 13 else{ 14 // 更新重叠气球,把这俩看作一个 15 points[i][1] = min(points[i][1],points[i-1][1]); 16 } 17 } 18 return result; 19 } 20 };