365天挑战LeetCode1000题——Day 033 二分查找 11

本文探讨了一种算法,Solution类中的maxProfitAssignment函数,用于在给定难度和利润的情况下,合理安排工作,最大化收益。通过计算难度等级和利润,然后根据员工技能匹配,确保每个任务最适合的人完成,从而实现整体效益的最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


826. 安排工作以达到最大收益

在这里插入图片描述

首刷自解

class Solution {
public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
        map<int, int> getProfit;
        int n = difficulty.size();
        int curMax = 0;
        for (int i = 0; i < n; i++) {
            if (getProfit.find(difficulty[i]) != getProfit.end())
                getProfit[difficulty[i]] = max(getProfit[difficulty[i]],
                profit[i]);
            else getProfit[difficulty[i]] = profit[i];
        }
        sort(difficulty.begin(), difficulty.end());
        for (int i = 0; i < n; i++) {
            curMax = max(curMax, getProfit[difficulty[i]]);
            getProfit[difficulty[i]] = curMax;
        }
        int ans = 0;
        for (int w : worker) {
            if (w < difficulty[0]) continue;
            auto it = lower_bound(difficulty.begin(), difficulty.end(), w);
            if (it == difficulty.end()) ans += curMax;
            else if (*it > w) {
                it--;
                ans += getProfit[*it];
            }
            else ans += getProfit[*it];
            // cout << ans << endl;
        }
        return ans;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值