FastPrepMinimum Seats for One-Way Car Pooling
Problem · Array

Minimum Seats for One-Way Car Pooling

Learn this problem
MediumSquadStack.ai logoSquadStack.aiNEW GRADOA

Problem statement

A car travels in one direction along a route. You are given an array stops, where each row is [passengers, from, to].

  • passengers people enter the car at point from.
  • The same people leave the car at point to.
  • At a point where both events occur, passengers leaving the car get out before new passengers enter.

Return the minimum number of seats the car needs so that every trip can be completed. This is the maximum number of passengers simultaneously in the car.

Function

minimumSeats(stops: int[][]) → int

Examples

Example 1

stops = [[2,1,5],[3,3,7]]return = 5

Between points 3 and 5, both groups are in the car, so 2 + 3 = 5 seats are required.

Example 2

stops = [[2,1,5],[3,5,7]]return = 3

At point 5, the first two passengers leave before the next three enter. The groups never overlap, so only 3 seats are needed.

Example 3

stops = [[4,0,10],[2,2,6],[5,6,8],[1,8,9]]return = 9

At point 6, two passengers leave before five enter. The occupancy becomes 4 + 5 = 9, the maximum along the route.

Constraints

  • 1 <= stops.length <= 10^5.
  • Every row in stops has exactly three integers.
  • 1 <= passengers <= 10^4.
  • 0 <= from < to <= 10^9.
  • The sum of all passengers values is at most 10^9.

More SquadStack.ai problems

drafts saved locally
public int minimumSeats(int[][] stops) {
  // Write your code here.
}
stops[[2,1,5],[3,3,7]]
expected5
checking account