代码随想录算法训练营第35天 | 860.柠檬水找零、406.根据身高重建队列、452. 用最少数量的箭引爆气球

发布于 2022-12-25  244 次阅读


AI 摘要

Excerpt: 代码随想录算法训练营第35天的三道题目分别是:860.柠檬水找零、406.根据身高重建队列、452.用最少数量的箭引爆气球。本文包含了三道题目的思路和AC代码,以及标准答案。柠檬水找零的思路是直接遍历一次数组,找零手中对应纸币数量要减少;根据身高重建队列的思路是先按照身高大小从高到低排序,然后遍历该数组,将每个元素按照第二个元素的位置重新插入一遍即可;用最少数量的箭引爆气球的思路是将气球按照结束位置从小到大排序,然后从左到右扫描,统计最少有多少个箭头能引爆所有的气球。

860. 柠檬水找零 - 力扣(LeetCode)

思路:

  • 直接遍历一次数组,遇到十块看看有没有五块找,遇到二十看看有没有10 + 5或者3 x 5即可,记得收钱手中相应纸币数量要增加,找零手中对应纸币数量要减少

我的AC代码

//时间复杂度O(n),空间复杂度O(1)
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;
    }
};

标准答案

//时间复杂度O(n),空间复杂度O(1)
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) {
                // 优先消耗10美元,因为5美元的找零用处更大,能多留着就多留着
                if (five > 0 && ten > 0) {
                    five--;
                    ten--;
                    twenty++; // 其实这行代码可以删了,因为记录20已经没有意义了,不会用20来找零
                } else if (five >= 3) {
                    five -= 3;
                    twenty++; // 同理,这行代码也可以删了
                } else return false;
            }
        }
        return true;
    }
};

406. 根据身高重建队列 - 力扣(LeetCode)

思路:

  • 先按照身高大小从高到低排序,身高相同的第二个属性小的排前面。然后遍历该数组,将每个元素按照第二个元素的位置重新插入一遍即可,因为身高已经按照从高到低排好,排在第几就说明前面有几个身高相同或者更高,而后面的都比后面小或者一样,不会影响结果

我的AC代码

使用vector(费时)

//时间复杂度O(nlog n + n^2),空间复杂度O(n)
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;
    }
};

使用链表

//时间复杂度O(nlog n + n^2),空间复杂度O(n)
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(费时)

//时间复杂度O(nlog n + n^2),空间复杂度O(n)
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;
    }
};

使用链表(省时)

//时间复杂度O(nlog n + n^2),空间复杂度O(n)
class Solution {
public:
    // 身高从大到小排(身高相同k小的站前面)
    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; // list底层是链表实现,插入效率比vector高的多
        for (int i = 0; i < people.size(); i++) {
            int position = people[i][1]; // 插入到下标为position的位置
            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. 用最少数量的箭引爆气球 - 力扣(LeetCode)

思路:

  • 先按照左侧起点从小到大排序,然后遍历数组。如果前后两个气球不重叠,那么射箭数加一,否则更新右侧区间结束值为两个气球中的较小值

我的AC代码

//时间复杂度O(nlogn),空间复杂度O(1)
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;
    }
};

标准答案

//时间复杂度O(nlogn),空间复杂度O(1)
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; // points 不为空至少需要一支箭
        for (int i = 1; i < points.size(); i++) {
            if (points[i][0] > points[i - 1][1]) {  // 气球i和气球i-1不挨着,注意这里不是>=
                result++; // 需要一支箭
            }
            else {  // 气球i和气球i-1挨着
                points[i][1] = min(points[i - 1][1], points[i][1]); // 更新重叠气球最小右边界
            }
        }
        return result;
    }
};