FastPrepTime of Maximum Mall Occupancy

Time of Maximum Mall Occupancy

Wayfair logoWayfair● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row of events is [count,timestamp,direction]. Direction 1 means that many people enter a mall; direction 0 means they exit. Return the recorded timestamp when the mall has its maximum occupancy.

For this exercise, assume the mall starts empty and rows may be unordered. All events with the same timestamp are one simultaneous batch. Measure occupancy only after the complete batch, without counting an intermediate peak caused by an ordering of its rows. Repeated records contribute separately.

For this exercise, assume aggregate occupancy never becomes negative after a timestamp batch. If several recorded timestamps share the maximum, return the smallest one. Empty input returns -1. Accumulate people counts with wide integer arithmetic.

Function

maxOccupancyTime(events: int[][]) → int

Examples

Example 1

events = [[12,1,1],[5,2,0],[7,1,1],[5,1,0]]return = 1

At timestamp 1, twelve plus seven enter and five exit, leaving fourteen. The exit at timestamp 2 leaves nine, so the maximum occurs at 1.

Example 2

events = [[2,9,1],[2,5,1],[2,8,0]]return = 5

The input is unordered. Occupancy is two at 5, zero at 8 and two at 9; the earliest maximum is 5.

Constraints

  • For this exercise, assume 0 <= events.length <= 1000. Every row has exactly three integers.
  • For this exercise, assume 1 <= count <= 1000000000, 0 <= timestamp <= 1000000000, and direction is 0 or 1.
  • The sum of all changes at or before any recorded timestamp is nonnegative. Occupancy may exceed signed 32-bit range.

More Wayfair problems

See Wayfair hiring insights
public int maxOccupancyTime(int[][] events) {
    // Write your code here.
    return -1;
}
events[[12,1,1],[5,2,0],[7,1,1],[5,1,0]]
expected1
Checking account…