Ctrl K

Orders by Skill

Medium
A workshop employs masters; masters[i] is the skill level of master i. Orders have come in: orders[j] = [need, pay] means order j requires a skill of exactly need and pays pay. Each master completes at most one order, and only one whose need equals their skill. In addition, one generalist is on shift today: they can take any single order regardless of its skill requirement. Each order can be completed at most once. Return the largest total pay you can earn.
Example 1
masters=[2, 2, 5], orders=[[2, 30], [2, 50], [2, 20], [5, 10], [7, 40]] → 130

The two skill-2 masters take the orders paying 50 and 30, the skill-5 master takes the one paying 10. The generalist takes the best remaining order, 40. Total: 130.

Example 2
masters=[3], orders=[[1, 8], [2, 9]] → 9

No order needs skill 3, and the generalist can take just one order, the one paying 9.

Example 3
masters=[4, 4], orders=[[4, 6], [4, 6], [4, 6]] → 18
Constraints
  • 1 ≤ len(masters) ≤ 10⁵
  • 1 ≤ masters[i] ≤ 10⁹
  • 1 ≤ len(orders) ≤ 10⁵
  • orders[j] = [need, pay], 1 ≤ need, pay ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumJobs With a Cooldown MediumSpread Out the Labels MediumCross Out Half the List MediumOne Event per Day Medium
loading Python…
Input
masters=[2, 2, 5], orders=[[2, 30], [2, 50], [2, 20], [5, 10], [7, 40]]
Expected
130

Run checks the first 3 tests in your browser. Submit checks all 207 tests on the server, hidden ones included.