Maximize Chef Assignment Profit
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates skill in array manipulation, data preprocessing, and algorithmic optimization for matching resources to tasks and aggregating profits.
Part 1: Maximum Total Profit from Chef Assignments
Constraints
- 0 <= len(chefSkill) <= 100000
- 0 <= len(dishDifficulty) == len(dishProfit) <= 100000
- 0 <= chefSkill[i], dishDifficulty[j], dishProfit[j] <= 1000000000
- The total profit may exceed 32-bit integer range.
Examples
Input: ([1, 2, 3], [1, 2, 3], [1, 2, 3])
Expected Output: 6
Explanation: Each chef can cook the dish with the matching difficulty, so the total is 1 + 2 + 3 = 6.
Input: ([5, 5, 5], [2, 4], [10, 50])
Expected Output: 150
Explanation: All three chefs can cook the difficulty-4 dish worth 50 profit, and the same dish can be reused.
Input: ([4, 2, 8], [2, 4, 2, 6], [10, 20, 15, 25])
Expected Output: 60
Explanation: Best profits are: skill 4 -> 20, skill 2 -> 15, skill 8 -> 25. Total = 60.
Input: ([1, 1], [2, 3], [10, 20])
Expected Output: 0
Explanation: No chef has enough skill to cook any dish.
Input: ([], [1, 2], [10, 20])
Expected Output: 0
Explanation: With no chefs, the total profit is 0.
Hints
- If you sort dishes by difficulty, you can track the best profit seen so far as difficulty increases.
- If you also sort the chefs by skill, you can scan through the dishes only once.
Part 2: Build Best Profit by Difficulty Array
Constraints
- 0 <= len(dishDifficulty) == len(dishProfit) <= 100000
- 0 <= dishDifficulty[j] <= 200000
- 0 <= dishProfit[j] <= 1000000000
Examples
Input: ([1, 2, 3], [1, 2, 3])
Expected Output: [0, 1, 2, 3]
Explanation: At each difficulty, the best available profit is the largest profit among dishes up to that difficulty.
Input: ([2, 4, 2, 5], [10, 20, 15, 5])
Expected Output: [0, 0, 15, 15, 20, 20]
Explanation: Difficulty 2 has two dishes, so keep the better profit 15. Then apply prefix maxima.
Input: ([1, 3, 5], [10, 5, 7])
Expected Output: [0, 10, 10, 10, 10, 10]
Explanation: Even though later dishes are less profitable, the prefix maximum keeps the best value seen so far.
Input: ([0, 2], [5, 1])
Expected Output: [5, 5, 5]
Explanation: A difficulty-0 dish is available, so every difficulty from 0 to 2 can achieve at least profit 5.
Input: ([], [])
Expected Output: [0]
Explanation: No dishes means the result is defined as [0].
Hints
- First store the best profit for each exact difficulty value.
- Then sweep from left to right and turn the array into a prefix maximum array.