news 2026/4/28 13:07:11

leetcode 826. Most Profit Assigning Work 安排工作以达到最大收益

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
leetcode 826. Most Profit Assigning Work 安排工作以达到最大收益

Problem: 826. Most Profit Assigning Work 安排工作以达到最大收益

解题过程

首先按照相同方式排序difficulty和profit,首先difficulty和索引放到一起排序,然后将profit的数值放到对应的地方,就相当按照difficulty排序的方式排序了profit,最后单独排序difficulty,对每个worker,二分查找上界最大值,对上界下面的拿到最大利润,累加即可的

Code

class Solution { public: int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) { vector<pair<int, int>> tr; int n = difficulty.size(), m = worker.size(); for(int i = 0; i < n; i++) { tr.push_back({difficulty[i], i}); } function<bool(pair<int, int>&, pair<int, int>&)> fun = [&](pair<int, int>& a, pair<int, int>& c) { return a.first < c.first; }; sort(tr.begin(), tr.end(), fun); sort(difficulty.begin(), difficulty.end()); vector<int> profitCP = profit; for(int i = 0; i < n; i++) { profitCP[i] = profit[tr[i].second]; } int ind, sum = 0; for(int i = 0; i < m; i++) { ind = upper_bound(difficulty.begin(), difficulty.end(), worker[i]) - difficulty.begin(); if(ind > 0) { sum += *max_element(profitCP.begin(), profitCP.begin() + ind); } } return sum; } };

官方题解的

class Solution { public: int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) { vector<pair<int, int>> tr; int n = difficulty.size(), m = worker.size(); for(int i = 0; i < n; i++) { tr.push_back({difficulty[i], profit[i]}); } function<bool(pair<int, int>&, pair<int, int>&)> fun = [&](pair<int, int>& a, pair<int, int>& c) { return a.first < c.first; }; sort(tr.begin(), tr.end(), fun); sort(worker.begin(), worker.end()); // sort(difficulty.begin(), difficulty.end()); // vector<int> profitCP = profit; // for(int i = 0; i < n; i++) { // profitCP[i] = profit[tr[i].second]; // } int ind, sum = 0, best = 0; int j = 0; for(int i = 0; i < m; i++) { while(j < n && tr[j].first <= worker[i]) { best = max(best, tr[j].second); j++; } sum += best; // ind = upper_bound(difficulty.begin(), difficulty.end(), worker[i]) - difficulty.begin(); // if(ind > 0) { // sum += *max_element(profitCP.begin(), profitCP.begin() + ind); // } } return sum; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/19 13:43:39

震撼发布!智能资源规划AI系统,引领AI应用架构师新潮流

震撼发布&#xff01;智能资源规划AI系统&#xff0c;引领AI应用架构师新潮流 一、引言&#xff1a;AI应用架构师的“资源规划之痛” 1.1 那些年&#xff0c;我们踩过的资源规划坑 作为AI应用架构师&#xff0c;你是否遇到过这样的场景&#xff1f; 峰值突发时的“手忙脚乱”&a…

作者头像 李华
网站建设 2026/4/22 16:56:28

Path of Exile 2物品过滤器终极配置完整指南

Path of Exile 2物品过滤器终极配置完整指南 【免费下载链接】NeverSink-Filter-for-PoE2 This is a lootfilter for the game "Path of Exile 2". It adds colors, sounds, map icons, beams to highlight remarkable gear and inform the user 项目地址: https:/…

作者头像 李华
网站建设 2026/4/26 14:10:55

为什么你的微服务总崩溃?可能是负载均衡配置少了这一步!

第一章&#xff1a;为什么你的微服务总崩溃&#xff1f;微服务架构虽提升了系统的灵活性与可扩展性&#xff0c;但也带来了更高的复杂性。许多团队在享受拆分带来的红利时&#xff0c;却忽视了服务间依赖、网络通信和故障传播等关键问题&#xff0c;最终导致系统频繁崩溃。缺乏…

作者头像 李华
网站建设 2026/4/26 2:35:35

Git分支策略:feature/release/hotfix规范

Git分支策略&#xff1a;feature/release/hotfix规范 在大模型与多模态系统日益复杂的今天&#xff0c;一个看似基础的工程实践——Git分支管理&#xff0c;正悄然决定着整个研发体系的稳定性与敏捷性。想象一下&#xff1a;你正在为下一代大语言模型训练框架集成全新的量化推理…

作者头像 李华
网站建设 2026/4/25 13:42:45

MuseGAN:AI音乐生成的革命性突破还是技术奇迹?

MuseGAN&#xff1a;AI音乐生成的革命性突破还是技术奇迹&#xff1f; 【免费下载链接】musegan An AI for Music Generation 项目地址: https://gitcode.com/gh_mirrors/mu/musegan MuseGAN作为音乐生成领域的开源项目&#xff0c;基于生成对抗网络技术实现了多轨道音乐…

作者头像 李华