思路:
- 直接遍历一次数组,遇到十块看看有没有五块找,遇到二十看看有没有
10 + 5
或者3 x 5
即可,记得收钱手中相应纸币数量要增加,找零手中对应纸币数量要减少
我的AC代码
| |
| class Solution { |
| public: |
| bool lemonadeChange(vector<int>& bills) { |
| vector<int> money(2, 0); |
| for(int i = 0; i < bills.size(); ++i) { |
| if(bills[i] == 10) { |
| if(money[0] < 1) { |
| return false; |
| } |
| else { |
| money[0]--; |
| money[1]++; |
| } |
| } |
| else if(bills[i] == 20) { |
| if(money[0] == 0) { |
| return false; |
| } |
| else if(money[1] == 0) { |
| if(money[0] < 3) { |
| return false; |
| } |
| else { |
| money[0] -= 3; |
| } |
| } |
| else { |
| money[0]--; |
| money[1]--; |
| } |
| } |
| else if(bills[i] == 5) { |
| money[0]++; |
| } |
| } |
| return true; |
| } |
| }; |
标准答案
| |
| class Solution { |
| public: |
| bool lemonadeChange(vector<int>& bills) { |
| int five = 0, ten = 0, twenty = 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--; |
| twenty++; |
| } else if (five >= 3) { |
| five -= 3; |
| twenty++; |
| } else return false; |
| } |
| } |
| return true; |
| } |
| }; |
思路:
- 先按照身高大小从高到低排序,身高相同的第二个属性小的排前面。然后遍历该数组,将每个元素按照第二个元素的位置重新插入一遍即可,因为身高已经按照从高到低排好,排在第几就说明前面有几个身高相同或者更高,而后面的都比后面小或者一样,不会影响结果
我的AC代码
使用vector(费时)
| |
| class Solution { |
| public: |
| static bool cmp(vector<int>& a, 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>> ans; |
| for(int i = 0; i < people.size(); ++i) { |
| ans.insert(ans.begin() + people[i][1], people[i]); |
| } |
| return ans; |
| } |
| }; |
使用链表
| |
| class Solution { |
| public: |
| static bool cmp(vector<int>& a, 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); |
| list<vector<int>> tmp; |
| for(int i = 0; i < people.size(); ++i) { |
| int cnt = people[i][1]; |
| std::list<vector<int>>::iterator it = tmp.begin(); |
| while(cnt--) { |
| it++; |
| } |
| tmp.insert(it, people[i]); |
| } |
| return vector<vector<int>>(tmp.begin(), tmp.end()); |
| } |
| }; |
标准答案
使用vector(费时)
| |
| 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>> que; |
| for (int i = 0; i < people.size(); i++) { |
| int position = people[i][1]; |
| que.insert(que.begin() + position, people[i]); |
| } |
| return que; |
| } |
| }; |
使用链表(省时)
| |
| 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); |
| 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()); |
| } |
| }; |
思路:
- 先按照左侧起点从小到大排序,然后遍历数组。如果前后两个气球不重叠,那么射箭数加一,否则更新右侧区间结束值为两个气球中的较小值
我的AC代码
| |
| 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]; |
| } |
| int findMinArrowShots(vector<vector<int>>& points) { |
| sort(points.begin(), points.end(), cmp); |
| int cnt = 1; |
| for(int i = 1; i < points.size(); ++i) { |
| if(points[i][0] > points[i - 1][1]) { |
| cnt++; |
| } |
| else { |
| points[i][1] = min(points[i - 1][1], points[i][1]); |
| } |
| } |
| return cnt; |
| } |
| }; |
标准答案
| |
| class Solution { |
| private: |
| static bool cmp(const vector<int>& a, const vector<int>& b) { |
| return a[0] < b[0]; |
| } |
| public: |
| int findMinArrowShots(vector<vector<int>>& points) { |
| if (points.size() == 0) return 0; |
| sort(points.begin(), points.end(), cmp); |
| |
| 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; |
| } |
| }; |
Comments NOTHING