Most Profit Assigning Work — LeetCode 826 Python Solution
- Problem
- #826
- Pattern
- Two Pointers
- Reading time
- 3 min
- Source
- leetcode.com
The problem
You have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where: difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]).
Example
- Input
- difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
- Output
- 100
- Explanation
- Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
Python solution
class Solution:
def maxProfitAssignment(
self, difficulty: List[int], profit: List[int], worker: List[int]
) -> int:
worker.sort()
jobs = sorted(zip(difficulty, profit))
ans = mx = i = 0
for w in worker:
while i < len(jobs) and jobs[i][0] <= w:
mx = max(mx, jobs[i][1])
i += 1
ans += mx
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n + m \times \log m) |
| Space | O(n) auxiliary |
Pattern: Two Pointers
Use the order already in the input to discard half the search space at every step. LeetCode 826. Most Profit Assigning Work is filed here because LeetCode tags it Two Pointers, which is the vocabulary this hub collects.
The two pointers guide has the Python template for the pattern and the 201 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 826. Most Profit Assigning Work?
- LeetCode 826. Most Profit Assigning Work is rated Medium on LeetCode.
- What is the time complexity of LeetCode 826. Most Profit Assigning Work?
- The Python solution on this page runs in O(n \times \log n + m \times \log m).
- What is the space complexity of LeetCode 826. Most Profit Assigning Work?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 826. Most Profit Assigning Work cover?
- LeetCode 826. Most Profit Assigning Work is tagged Greedy, Array, Two Pointers, Binary Search and Sorting on LeetCode.