FastPrepParking-Lot Occupancy Timeline

Parking-Lot Occupancy Timeline

Google logoGoogle● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Each parking record is an integer pair [enter, exit]. A vehicle occupies the lot at integer time x exactly when enter <= x < exit.

Given the records and a non-negative integer t, return the number of vehicles in the lot at every integer time from 0 through t, inclusive.

Records may begin before zero or end after t. A zero-duration record contributes nothing.

Function

occupancyTimeline(records: int[][], t: int) → int[]

Examples

Example 1

records = [[1,3],[2,5],[4,5]]t = 4return = [0,1,2,1,2]

The half-open intervals are active at the listed integer times.

Example 2

records = [[-2,1],[0,3],[3,3]]t = 3return = [2,1,1,0]

The first record is still active at time zero, and the zero-duration record contributes nothing.

Example 3

records = []t = 2return = [0,0,0]

An empty record set leaves the lot empty.

Constraints

  • 0 <= records.length <= 200000.
  • Each row has exactly two integers and satisfies enter <= exit.
  • 0 <= t <= 1000000.
  • Times lie in the signed 32-bit range.

More Google problems

See Google hiring insights
public int[] occupancyTimeline(int[][] records, int t) {
    // Write your code here.
}
records[[1,3],[2,5],[4,5]]
t4
expected[0,1,2,1,2]
Checking account…