LeetCode 826 Most Profit Assigning Work All In One
LeetCode 826 Most Profit Assigning Work All In One
- Most Profit Assigning Work
difficulty: Medium
/ 难度: 中等
solutions
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
let sum = 0;
let arr = [];
for(let i = 0; i < difficulty.length; i++) {
arr.push({
d: difficulty[i],
p: profit[i],
});
}
// 按困难排序
arr.sort((a, b) => a.d - b.d);
difficulty.sort((a, b) => a - b)[0];
worker.sort((a, b) => a - b)[worker.length - 1];
// let minDifficulty = difficulty.sort((a, b) => a - b)[0];
// let maxWorker = worker.sort((a, b) => a - b)[worker.length - 1];
// if(minDifficulty > maxWorker) {
// return 0;
// }
for(let i = 0; i < worker.length; i++) {
let j = 0;
let temp = 0;
// ✅ 条件的先后顺序很重要 ❗️
while(j < arr.length && arr[j].d <= worker[i]) {
// if(minDifficulty > worker[i]) {
// break;
// }
if(temp < arr[j].p) {
temp = arr[j].p;
}
j++;
}
sum += temp
}
return sum;
};
demos
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
// let minDifficulty = difficulty.sort((a, b) => a - b)[0];
// let maxWorker = worker.sort((a, b) => a - b)[worker.length - 1];
// if(minDifficulty > maxWorker) {
// return 0;
// }
let sum = 0;
let arr = [];
for(let i = 0; i < difficulty.length; i++) {
// console.log(`❌ =`, difficulty[i], profit[i])
arr.push({
d: difficulty[i],
p: profit[i],
});
}
// 按困难排序
arr.sort((a, b) => a.d - b.d);
console.log(`arr =`, arr)
// 排序顺序 bug
difficulty.sort((a, b) => a - b)[0];
worker.sort((a, b) => a - b)[worker.length - 1];
// let minDifficulty = difficulty.sort((a, b) => a - b)[0];
// let maxWorker = worker.sort((a, b) => a - b)[worker.length - 1];
// if(minDifficulty > maxWorker) {
// return 0;
// }
for(let i = 0; i < worker.length; i++) {
let j = 0;
let temp = 0;
// ❌
// while(arr[j].d <= worker[i] && j < arr.length - 1) {
// ❌ TypeError: Cannot read properties of undefined (reading 'd')
// while(arr[j].d <= worker[i] && j < arr.length) {
// ✅ 条件的先后顺序很重要 ❗️
while(j < arr.length && arr[j].d <= worker[i]) {
// if(minDifficulty > worker[i]) {
// break;
// }
if(temp < arr[j].p) {
temp = arr[j].p;
}
j++;
}
// console.log(`✅ worker[i] = `, worker[i])
console.log(`❓ temp = `, temp)
sum += temp
}
return sum;
};
/*
Wrong Answer
52 / 57 testcases passed
Input
difficulty =
[5,50,92,21,24,70,17,63,30,53]
profit =
[68,100,3,99,56,43,26,93,55,25]
worker =
[96,3,55,30,11,58,68,36,26,1]
Output
768
Expected
765
*/
/*
Wrong Answer
22 / 57 testcases passed
Input
difficulty =
[13,37,58]
profit =
[4,90,96]
worker =
[34,73,45]
Stdout
❓ temp = 4
❓ temp = 90
❓ temp = 90
Output
184
Expected
190
*/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18255498
未经授权禁止转载,违者必究!