1108. Intern Matching

Hard7.5 points estimatedGraphDepth-First SearchGreedy3 hints

A company is placing summer interns on projects. There are left interns, numbered 0 to left - 1, and right projects, numbered 0 to right - 1. Each entry [i, p] of edges says that intern i has the skills for project p. The same pair may be listed more than once.

An intern can be assigned to at most one project, and a project can take at most one intern. Interns may be left without a project, and projects may stay empty. Return the largest number of interns that can be assigned at the same time. Note that giving every intern their first available project is not always the best plan: an early choice may have to be undone to make room for someone else.

Example 1

Input:
left = 3, right = 3, edges = [[0,0],[0,1],[1,0],[2,1]]
Output:
2
Explanation:

Only projects 0 and 1 are reachable by anyone, so at most two interns can be placed, for example intern 1 on project 0 and intern 2 on project 1.

Example 2

Input:
left = 2, right = 2, edges = [[0,0],[0,1],[1,0]]
Output:
2
Explanation:

Giving intern 0 project 0 would leave intern 1 with nothing; moving intern 0 to project 1 lets both work.

Constraints

1 ≤ left, right ≤ 104

0 ≤ edges.length ≤ 3 * 104

edges[k] = [i, p] with 0 ≤ i < left and 0 ≤ p < right; duplicate pairs are allowed

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,800 msC++ 700 msJava 1,400 msJavaScript 1,400 msTypeScript 1,400 ms

Expected complexity

Time
O(V * E) worst case, near linear in practice
Space
O(V + E)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…