---
# System prepended metadata

title: 0826. Most Profit Assigning Work
tags: [Leetcode, Two Pointers, Sorting, Medium, Greedy]

---

# 0826. Most Profit Assigning Work
###### tags: `Leetcode` `Medium` `Greedy` `Sorting` `Two Pointers`
Link: https://leetcode.com/problems/most-profit-assigning-work/description/
## 思路
先对(```difficulty```, ```profit```) pair排序 然后对```worker```排序
然后用双指针找答案
## Code
```java=
class Solution {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        int ans = 0;
        int m = difficulty.length;
        List<int[]> tasks = new ArrayList<>();
        for(int i=0; i<m; i++){
            tasks.add(new int[]{difficulty[i], profit[i]});
        }
        Collections.sort(tasks, (a,b)->(a[0]-b[0]));
        Arrays.sort(worker);
        int i=0, best = 0;
        for(int ability:worker){
            while(i<m && ability>=tasks.get(i)[0]){
                best = Math.max(best, tasks.get(i)[1]);
                i++;
            }
            ans+=best;
        }
        return ans;
    }
}
```