news 2026/5/1 7:31:01

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/25 9:21:30

C++开发者必备:2024年最全开源框架与库终极指南

C开发者必备&#xff1a;2024年最全开源框架与库终极指南 【免费下载链接】awesome-cpp awesome-cpp - 一个精选的 C 框架、库、资源和有趣事物的列表。 项目地址: https://gitcode.com/GitHub_Trending/aw/awesome-cpp 在当今快速发展的软件开发领域&#xff0c;C依然保…

作者头像 李华
网站建设 2026/5/1 6:55:28

Swagger UI展示模型服务接口文档

Swagger UI展示模型服务接口文档 在当今大模型快速迭代的背景下&#xff0c;一个常见的工程难题浮出水面&#xff1a;如何让团队成员快速理解并调用复杂的模型服务&#xff1f;许多项目依然依赖口头沟通或零散的笔记来说明API用法&#xff0c;导致协作效率低下、集成成本高昂。…

作者头像 李华
网站建设 2026/4/29 5:48:54

Simple Comic:Mac上最流畅的漫画阅读器完整指南

Simple Comic&#xff1a;Mac上最流畅的漫画阅读器完整指南 【免费下载链接】Simple-Comic OS X comic viewer 项目地址: https://gitcode.com/gh_mirrors/si/Simple-Comic 如果你是一位Mac用户&#xff0c;同时又是漫画爱好者&#xff0c;那么Simple Comic绝对是你不可…

作者头像 李华
网站建设 2026/4/28 18:18:40

谷歌镜像访问不稳定?本地部署才是王道选择

谷歌镜像访问不稳定&#xff1f;本地部署才是王道选择 在大模型研发的日常中&#xff0c;你是否经历过这样的场景&#xff1a;凌晨两点&#xff0c;终于调通了训练脚本&#xff0c;兴冲冲地启动 git clone 或 huggingface-cli download&#xff0c;结果卡在 30% 进度条上一动不…

作者头像 李华
网站建设 2026/4/14 20:45:23

Sionna通信仿真:3个步骤让新手快速搭建专业级无线通信系统

Sionna通信仿真&#xff1a;3个步骤让新手快速搭建专业级无线通信系统 【免费下载链接】sionna Sionna: An Open-Source Library for Next-Generation Physical Layer Research 项目地址: https://gitcode.com/gh_mirrors/si/sionna 你是不是曾经被复杂的通信系统仿真搞…

作者头像 李华
网站建设 2026/5/1 7:29:02

图像翻译技术5大突破:从理论到实践的完整指南

图像翻译技术5大突破&#xff1a;从理论到实践的完整指南 【免费下载链接】awesome-image-translation A collection of awesome resources image-to-image translation. 项目地址: https://gitcode.com/gh_mirrors/aw/awesome-image-translation 想要让照片瞬间变换风格…

作者头像 李华