Ctrl K

Parking Between Posts

Hard
A street runs along an infinite number line that starts at 0. The events in requests happen one after another: - [1, x]: a post is put up at point x. There is guaranteed to be no post there yet; - [2, x, size]: report whether a car of length size could be parked entirely within the segment [0, x] with no post strictly inside the car. The car's ends may touch a post. The car is not actually parked, so the checks are independent. Return a list of True/False answers to all type-2 requests, in the order they come.
Example 1
requests=[[1, 4], [2, 6, 4], [2, 6, 5], [1, 1], [2, 6, 4]] → [True, False, False]

With a post at 4, the segment [0, 6] has free stretches of length 4 and 2: a car of length 4 fits, a car of length 5 does not. After a post at 1 the stretches are 1, 3 and 2, so length 4 no longer fits.

Example 2
requests=[[2, 3, 3], [1, 2], [2, 3, 3], [2, 9, 3]] → [True, False, True]

At first there are no posts and a car of length 3 fills all of [0, 3]. A post at 2 breaks that. On [0, 9] the stretch from 2 to 9 is free, so there is room.

Constraints
  • 1 ≤ len(requests) ≤ 1.5·10⁵
  • A type-1 request is [1, x], a type-2 request is [2, x, size]
  • 1 ≤ x, size ≤ min(5·10⁴, 3·len(requests))
  • No point gets a post more than once
  • There is at least one type-2 request
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardCost of Filling a Sorted Shelf HardTriples Both Rankings Agree On Hard
loading Python…
Input
requests=[[1, 4], [2, 6, 4], [2, 6, 5], [1, 1], [2, 6, 4]]
Expected
[True, False, False]

Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.