代码随想录30|860.柠檬水找零,406.根据身高重建队列

860.柠檬水找零

链接地址

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five = 0;
        int ten = 0;
        int tewnty = 0;
        for (int bill: bills) {
            if (bill == 5) five++;
            if (bill == 10) {
                if (five == 0) return false;
                else {
                    five--,ten++;
                }
            }
            if (bill == 20) {
                if (five > 0 && ten > 0) {
                    five--,ten--,tewnty++;
                } else if (five >= 3) {
                    five -= 3;
                    tewnty++;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
};

406.根据身高重建队列

链接地址

class Solution {
private:
    static bool cmp(const vector<int> a, const vector<int> b) {
        if (a[0] != b[0]) {
            return a[0] > b[0];
        }
        return a[1] < b[1];//身高相同,按照k值从小到大。
    }

public:
    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;
    }
};

你可能感兴趣的:(代码随想录刷题,算法,数据结构,c++)