928. Garage Slots

Easy1.5 points estimatedDesignSimulationCounting3 hints

A small parking garage has three kinds of bays: big bays for big cars, medium bays for medium cars and small bays for small cars. Build the class GarageSlots. Its constructor receives how many bays of each kind exist.

addCar(carType) tries to park a car, where carType is 1 for a big car, 2 for a medium car and 3 for a small car. A car may only use a bay of its own kind. If such a bay is still free, take it and return true; otherwise return false. Cars never leave. Every call must run in constant time.

Example 1

Input:
operations = ["GarageSlots","addCar","addCar","addCar","addCar"]arguments = [[1,1,0],[1],[2],[3],[1]]
Output:
[null,true,true,false,false]
Explanation:

The big slot takes the first car and the medium slot takes the second; no small slot exists for the third, and the big slot is already full for the fourth.

Example 2

Input:
operations = ["GarageSlots","addCar","addCar","addCar"]arguments = [[0,0,2],[3],[3],[3]]
Output:
[null,true,true,false]
Explanation:

Only the two small slots exist, so the third small car is turned away.

Constraints

0 ≤ big, medium, small ≤ 1000

carType is 1, 2 or 3

At most 1000 calls to addCar

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Input
Each case is an operation log. operations names the class first and then each method call; arguments holds the arguments for each, in the same order. Your answer is one list with a result per operation - null for the constructor and for methods that return nothing.

Expected complexity

Time
O(1)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…

operations names the class, then each method to call; arguments holds one list of arguments per operation, in the same order.