Problem

Maximum Comfortable Riders

FULLTIMEPHONE SCREEN
See Uber online assessment and hiring insights

There are n riders and one car with unlimited capacity. Each rider has a comfort range for how many other riders they are willing to share the car with.

You are given ranges, where ranges[i] = [minOthers, maxOthers] means rider i will ride only if the final car contains at least minOthers and at most maxOthers other riders.

Return the maximum number of riders that can be placed in the car so that every chosen rider is comfortable with the final group size.

Examples
01 · Example 1
ranges = [[0,2],[1,3],[2,4],[3,5]]
return = 3

If 3 riders are chosen, each chosen rider has 2 other riders in the car. The first three riders are all comfortable with 2 others. Choosing 4 riders would require 4 riders who are comfortable with 3 others, but only three riders satisfy that.

02 · Example 2
ranges = [[0,0],[0,0],[1,2]]
return = 1

Two riders are comfortable riding alone, so a group of size 1 is possible. No group of size 2 is possible because only one rider is comfortable with exactly one other rider.

Constraints
  • n == ranges.length
  • ranges[i].length == 2
  • 0 <= ranges[i][0] <= ranges[i][1]
  • ranges[i][1] <= n - 1
More Uber problems
drafts saved locally
public int maxComfortableRiders(int[][] ranges) {
  // write your code here
}
ranges[[0,2],[1,3],[2,4],[3,5]]
expected3
sign in to submit