Skip to content

Commit ae67402

Browse files
committed
一刷826
1 parent 0304aaa commit ae67402

5 files changed

Lines changed: 108 additions & 26 deletions

File tree

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5807,13 +5807,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
58075807
|Medium
58085808
|
58095809

5810-
//|{counter:codes}
5811-
//|{leetcode_base_url}/most-profit-assigning-work/[826. Most Profit Assigning Work^]
5812-
//|{source_base_url}/_0826_MostProfitAssigningWork.java[Java]
5813-
//|{doc_base_url}/0826-most-profit-assigning-work.adoc[题解]
5814-
//|Medium
5815-
//|
5816-
//
5810+
|{counter:codes}
5811+
|{leetcode_base_url}/most-profit-assigning-work/[826. Most Profit Assigning Work^]
5812+
|{source_base_url}/_0826_MostProfitAssigningWork.java[Java]
5813+
|{doc_base_url}/0826-most-profit-assigning-work.adoc[题解]
5814+
|Medium
5815+
|
5816+
58175817
//|{counter:codes}
58185818
//|{leetcode_base_url}/making-a-large-island/[827. Making A Large Island^]
58195819
//|{source_base_url}/_0827_MakingALargeIsland.java[Java]
Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,74 @@
11
[#0826-most-profit-assigning-work]
2-
= 826. Most Profit Assigning Work
2+
= 826. 安排工作以达到最大收益
33

4-
{leetcode}/problems/most-profit-assigning-work/[LeetCode - Most Profit Assigning Work^]
4+
https://leetcode.cn/problems/most-profit-assigning-work/[LeetCode - 826. 安排工作以达到最大收益^]
55

6-
We have jobs: `difficulty[i]` is the difficulty of the `i`th job, and `profit[i]` is the profit of the `i`th job.
6+
你有 `n` 个工作和 `m` 个工人。给定三个数组: `difficulty`, `profit``worker`,其中:
77

8-
Now we have some workers. `worker[i]` is the ability of the `i`th worker, which means that this worker can only complete a job with difficulty at most `worker[i]`.
8+
* `difficulty[i]` 表示第 `i` 个工作的难度,`profit[i]` 表示第 `i` 个工作的收益。
9+
* `worker[i]` 是第 `i` 个工人的能力,即该工人只能完成难度小于等于 `worker[i]` 的工作。
910
10-
Every worker can be assigned at most one job, but one job can be completed multiple times.
11+
每个工人 *最多* 只能安排 *一个* 工作,但是一个工作可以 *完成多次*
1112

12-
For example, if 3 people attempt the same job that pays $1, then the total profit will be $3. If a worker cannot complete any job, his profit is $0.
13+
* 举个例子,如果 3 个工人都尝试完成一份报酬为 `$1` 的同样工作,那么总收益为 `$3` 。如果一个工人不能完成任何工作,他的收益为 `$0`
1314
14-
What is the most profit we can make?
15+
返回 _在把工人分配到工作岗位后,我们所能获得的最大利润_
1516

16-
*Example 1:*
17+
*示例 1:*
1718

18-
[subs="verbatim,quotes,macros"]
19-
----
20-
*Input:* difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
21-
*Output:* 100
22-
*Explanation:* Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.
23-
----
19+
....
20+
输入: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
21+
输出: 100
22+
解释: 工人被分配的工作难度是 [4,4,6,6] ,分别获得 [20,20,30,30] 的收益。
23+
....
24+
25+
*示例 2:*
26+
27+
....
28+
输入: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
29+
输出: 0
30+
....
2431

25-
*Notes:*
32+
*提示:*
2633

34+
* `n == difficulty.length`
35+
* `n == profit.length`
36+
* `m == worker.length`
37+
* `1 \<= n, m \<= 10^4^`
38+
* `1 \<= difficulty[i], profit[i], worker[i] \<= 10^5^`
2739
28-
* `1 <= difficulty.length = profit.length <= 10000`
29-
* `1 <= worker.length <= 10000`
30-
* `difficulty[i], profit[i], worker[i]` are in range `[1, 10^5]`
3140
3241
42+
== 思路分析
3343

44+
先将 `worker` 排序,然后对 `profit` 和 `difficulty` 排序: `profit` 优先,相同则再根据 `difficulty` 排序。从后向前遍历 `worker`,只要 `difficulty` 满足,优先取获利最大的工作。
45+
46+
TIP: 有点蒙对的感觉。不如题解更有说服力。
3447

3548
[[src-0826]]
49+
[tabs]
50+
====
51+
一刷::
52+
+
53+
--
3654
[{java_src_attr}]
3755
----
3856
include::{sourcedir}/_0826_MostProfitAssigningWork.java[tag=answer]
3957
----
58+
--
59+
60+
// 二刷::
61+
// +
62+
// --
63+
// [{java_src_attr}]
64+
// ----
65+
// include::{sourcedir}/_0826_MostProfitAssigningWork_2.java[tag=answer]
66+
// ----
67+
// --
68+
====
69+
70+
71+
== 参考资料
4072

73+
. https://leetcode.cn/problems/most-profit-assigning-work/solutions/2780326/pai-xu-shuang-zhi-zhen-pythonjavacgojsru-gthg/[826. 安排工作以达到最大收益 - 排序+双指针^]
74+
. https://leetcode.cn/problems/most-profit-assigning-work/solutions/2776977/an-pai-gong-zuo-yi-da-dao-zui-da-shou-yi-c0s1/[826. 安排工作以达到最大收益 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ include::0824-goat-latin.adoc[leveloffset=+1]
17441744

17451745
include::0825-friends-of-appropriate-ages.adoc[leveloffset=+1]
17461746

1747-
// include::0826-most-profit-assigning-work.adoc[leveloffset=+1]
1747+
include::0826-most-profit-assigning-work.adoc[leveloffset=+1]
17481748

17491749
// include::0827-making-a-large-island.adoc[leveloffset=+1]
17501750

logbook/202601.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,10 @@ endif::[]
10411041
|{doc_base_url}/0825-friends-of-appropriate-ages.adoc[题解]
10421042
|❌ 滑动窗口。要仔细分析题目要求,根据要求筛选出合适的解题策略。
10431043

1044+
|{counter:codes}
1045+
|{leetcode_base_url}/most-profit-assigning-work/[826. 安排工作以达到最大收益^]
1046+
|{doc_base_url}/0826-most-profit-assigning-work.adoc[题解]
1047+
|✅ 排序+双指针。对入参排序,优先获取获利最大的工作,累加即可。有点蒙对的感觉。不如题解更有说服力。
10441048

10451049

10461050

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
public class _0826_MostProfitAssigningWork {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2026-07-19 21:59:47
12+
*/
13+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
14+
Arrays.sort(worker);
15+
int n = difficulty.length;
16+
int m = worker.length;
17+
Integer[] index = new Integer[n];
18+
for (int i = 0; i < n; i++) {
19+
index[i] = i;
20+
}
21+
// 这样能通过验证,但是感觉有点蒙对了的感觉。
22+
Arrays.sort(index,
23+
Comparator.comparingInt((Integer a) -> profit[a])
24+
.thenComparingInt(a -> difficulty[a]));
25+
int result = 0;
26+
int idx = n - 1;
27+
for (int i = m - 1; i >= 0; i--) {
28+
while (idx >= 0 && difficulty[index[idx]] > worker[i]) {
29+
idx--;
30+
}
31+
if (idx >= 0) {
32+
result += profit[index[idx]];
33+
}
34+
}
35+
return result;
36+
}
37+
// end::answer[]
38+
39+
static void main() {
40+
new _0826_MostProfitAssigningWork()
41+
// .maxProfitAssignment(new int[]{85, 47, 57}, new int[]{24, 66, 99}, new int[]{40, 25, 25});
42+
.maxProfitAssignment(new int[]{68, 35, 52, 47, 86}, new int[]{67, 17, 1, 81, 3}, new int[]{92, 10, 85, 84, 82});
43+
}
44+
}

0 commit comments

Comments
 (0)