Problem · Graph

Doctor Appointment Slot Assignment

Learn this problem
MediumDRWOA

Problem statement

There are N patients, numbered from 0 to N - 1, who want to visit the doctor. The doctor has S possible appointment slots, numbered from 1 to S.

Each patient has two preferred slots. Patient K would like to visit the doctor during either slot A[K] or slot B[K].

The doctor can treat only one patient during each slot.

Return whether it is possible to assign every patient to one of their preferred slots so that at most one patient is assigned to each slot.

Function

solution(A: int[], B: int[], S: int) → boolean

Examples

Example 1

A = [1,1,3]B = [2,2,1]S = 3return = true

One valid assignment is [1,2,3], where the K-th element is the slot assigned to patient K. Another valid assignment is [2,1,3].

Example 2

A = [3,2,3,1]B = [1,3,1,2]S = 3return = false

There are four patients but only three slots, so at least two patients would have to share a slot.

Example 3

A = [2,5,6,5]B = [5,4,2,2]S = 8return = true

One valid assignment is [5,4,6,2].

Example 4

A = [1,2,1,6,8,7,8]B = [2,3,4,7,7,8,7]S = 10return = false

It is not possible to assign all patients to preferred slots while keeping at most one patient in each slot.

Constraints

  • N is an integer within the range [1, 100000].
  • S is an integer within the range [2, 100000].
  • Each element of arrays A and B is an integer within the range [1, S].
  • No patient has two preferences for the same slot, i.e. A[i] != B[i].

More DRW problems

drafts saved locally
public boolean solution(int[] A, int[] B, int S) {
  // write your code here
}
A[1,1,3]
B[2,2,1]
S3
expectedtrue
checking account