【第35天| 贪心4| ● 860.柠檬水找零 ● 406.根据身高重建队列 ● 452. 用最少数量的箭引爆气球】

860.柠檬水找零

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int bill_5 =0;
        int bill_10 = 0;
        for(int i=0; i<bills.size();i++)
        {
            if(bills[i]==5) bill_5++;
            else if(bills[i] == 10)
            {
                bill_10++;
                bill_5--;
                if(bill_5<0) return false;
            }
            else if(bills[i]==20)
            {
                if(bill_10>0)
                {
                    bill_10--;
                    bill_5--;
                }
                else bill_5 -= 3;
                if(bill_5<0) return false;
            }
        }
        return true;
    }
};

406.根据身高重建队列

class Solution {
    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];
    }
public:
    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. 用最少数量的箭引爆气球

class Solution {
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if (points.size() == 0) return 0;
        sort(points.begin(), points.end(), [](vector<int>& x, vector<int>& y){return x[0]<y[0];});

        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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图着色问题是指给定一个无向图,尝试用最少的颜色对每个顶点进行染色,使得任意两个相邻的顶点颜色不同。这是一个经典的NP难问题,没有有效的多项式时间算法,但是可以使用贪心算法来近似解决。 贪心算法的基本思路是:首先按照某种规则选择一个顶点进行染色,然后依次选择其它未染色的顶点,并尝试用最少的颜色进行染色。如果当前顶点的相邻顶点都已经染过色,那么就选择一个未使用的颜色进行染色,否则选择一个不与相邻顶点颜色相同的颜色进行染色。 C++代码实现如下: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; const int MAXN = 100; // 最大顶点数 int n, m; // n 表示顶点数,m 表示边数 vector<int> G[MAXN]; // 存储图的邻接表 int color[MAXN]; // 存储每个顶点的颜色 int greedy_coloring() { int ans = 0; for (int u = 0; u < n; ++u) { bool used[MAXN] = { false }; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (color[v] != -1) { used[color[v]] = true; } } for (int i = 0; ; ++i) { if (!used[i]) { color[u] = i; ans = max(ans, i); break; } } } return ans + 1; // 返回使用的颜色数 } int main() { cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } fill(color, color + n, -1); cout << "使用的颜色数:" << greedy_coloring() << endl; for (int i = 0; i < n; ++i) { cout << "顶点 " << i << " 的颜色是 " << color[i] << endl; } return 0; } ``` 该代码中,我们使用邻接表存储图,并使用一个数组 `color` 存储每个顶点的颜色,初始化为 -1 表示未染色。在 `greedy_coloring` 函数中,我们依次遍历每个顶点,对于每个未染色的顶点,我们枚举可用的颜色,选取第一个未被使用的颜色进行染色。在枚举颜色的过程中,我们需要检查相邻顶点的颜色,标记已经使用的颜色。 该算法的时间复杂度为 $O(n^2)$,因为对于每个顶点,都需要遍历其相邻顶点。当然,使用邻接表存储图可以优化到 $O(m)$ 的时间复杂度。该算法是一种近似算法,不一定能够得到最优解,但是在实际应用中效果很好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值