Problem · Graph

Longest Chain Booking

Learn this problem
MediumAirbnb logoAirbnbFULLTIMEPHONE SCREEN

Problem statement

There are bookingCount bookings numbered from 0 through bookingCount - 1. Each directed pair [u, v] in canFollow means booking v may immediately follow booking u in one valid chain.

The compatibility graph is a directed acyclic graph. A chain may use each booking at most once. Return the maximum number of bookings in any valid chain. A single booking is a valid chain.

Function

longestBookingChain(bookingCount: int, canFollow: int[][]) → int

Examples

Example 1

bookingCount = 5canFollow = [[0,1],[1,2],[0,3],[3,4]]return = 3

Both 0 -> 1 -> 2 and 0 -> 3 -> 4 contain three bookings.

Example 2

bookingCount = 4canFollow = []return = 1

With no compatibility edges, every longest chain contains one booking.

Example 3

bookingCount = 6canFollow = [[0,2],[1,2],[2,3],[2,4],[4,5]]return = 4

A longest chain is 0 -> 2 -> 4 -> 5.

Constraints

  • 1 <= bookingCount <= 100000.
  • 0 <= canFollow.length <= 200000.
  • Every pair is [u, v] with 0 <= u, v < bookingCount and u != v.
  • The directed graph contains no cycle.
  • Duplicate edges may appear and have no additional effect.

More Airbnb problems

drafts saved locally
public int longestBookingChain(int bookingCount, int[][] canFollow) {
  // Write your code here.
}
bookingCount5
canFollow[[0,1],[1,2],[0,3],[3,4]]
expected3
checking account