news 2026/8/29 3:21:12

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/8/27 2:06:57

Code Review模板:提升团队沟通效率

Code Review模板&#xff1a;提升团队沟通效率 在大模型开发日益普及的今天&#xff0c;一个常见的场景是&#xff1a;工程师提交了一套微调脚本&#xff0c;评审人却花了整整半天才搞清楚他到底改了哪些模块、用了什么并行策略、是否启用了量化——更糟糕的是&#xff0c;代码…

作者头像 李华
网站建设 2026/8/26 13:01:39

Drogon框架深度解析:从异步原理到高并发实战

Drogon框架深度解析&#xff1a;从异步原理到高并发实战 【免费下载链接】drogon 项目地址: https://gitcode.com/gh_mirrors/dro/drogon 让我们一起揭秘Drogon这个基于C17的高性能Web框架背后的技术奥秘。为什么在众多Web框架中Drogon能够脱颖而出&#xff1f;它如何通…

作者头像 李华
网站建设 2026/8/28 11:06:41

AI Agent自治系统离我们还有多远?

AI Agent自治系统离我们还有多远&#xff1f; 在今天&#xff0c;当你对着语音助手说“帮我订一张明天去上海的高铁票”&#xff0c;它不仅能听懂你的指令&#xff0c;还能自动打开购票App、查询车次、填写信息&#xff0c;甚至提醒你带身份证——这已经不再是科幻电影的情节。…

作者头像 李华
网站建设 2026/8/28 14:02:38

NeverSink过滤器:PoE2玩家的智能寻宝伙伴

你的游戏困扰诊断报告 【免费下载链接】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://gitcode.com/gh_mirro…

作者头像 李华
网站建设 2026/8/24 23:04:11

DiffPDF V6.0.0实战指南:精准识别PDF文档差异的高效解决方案

DiffPDF V6.0.0实战指南&#xff1a;精准识别PDF文档差异的高效解决方案 【免费下载链接】DiffPDFV6.0.0强大的PDF文件比较工具 DiffPDF V6.0.0 是一款功能强大的PDF文件比较工具&#xff0c;专为高效识别和展示PDF文件间的文本与布局差异而设计。无论是软件开发中的版本更新&a…

作者头像 李华