Design Parking System

🏠 ⬅️ ➡️

Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

Implement the ParkingSystem class:

  • ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
  • bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.

Example 1:

Input [ "ParkingSystem ", "addCar ", "addCar ", "addCar ", "addCar "] [[1, 1, 0], [1], [2], [3], [1]] Output [null, true, true, false, false]

Explanation ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); parkingSystem.addCar(1); // return true because there is 1 available slot for a big car parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car parkingSystem.addCar(3); // return false because there is no available slot for a small car parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

Constraints:

  • 0 <= big, medium, small <= 1000
  • carType is 1, 2, or 3
  • At most 1000 calls will be made to addCar

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    use ParkingSystem_mod
    implicit none

    type(ParkingSystem) :: parkingSystem
    integer :: i

    ! Example 1
    call parkingSystem%init(1, 1, 0)
    print '(L1)', parkingSystem%addCar(1) ! true
    print '(L1)', parkingSystem%addCar(2) ! true
    print '(L1)', parkingSystem%addCar(3) ! false
    print '(L1)', parkingSystem%addCar(1) ! false

contains

subroutine init(this, big, medium, small)
    class(ParkingSystem), intent(inout) :: this
    integer, intent(in) :: big, medium, small

    this%big = big
    this%medium = medium
    this%small = small
end subroutine init

function addCar(this, carType) result(res)
    class(ParkingSystem), intent(inout) :: this
    integer, intent(in) :: carType
    logical :: res

    select case (carType)
        case (1)
            if (this%big > 0) then
                this%big = this%big - 1
                res = .true.
            else
                res = .false.
            end if
        case (2)
            if (this%medium > 0) then
                this%medium = this%medium - 1
                res = .true.
            else
                res = .false.
            end if
        case (3)
            if (this%small > 0) then
                this%small = this%small - 1
                res = .true.
            else
                res = .false.
            end if
        case default
            error stop 'Invalid car type'
    end select
end function addCar

end program main

module ParkingSystem_mod
    implicit none

    private
    public :: ParkingSystem

    type :: ParkingSystem
        private
        integer :: big, medium, small
    contains
        procedure, pass :: init => init
        procedure, pass :: addCar => addCar
    end type ParkingSystem

contains

subroutine init(this, big, medium, small)
    class(ParkingSystem), intent(inout) :: this
    integer, intent(in) :: big, medium, small

    this%big = big
    this%medium = medium
    this%small = small
end subroutine init

function addCar(this, carType) result(res)
    class(ParkingSystem), intent(inout) :: this
    integer, intent(in) :: carType
    logical :: res

    select case (carType)
        case (1)
            if (this%big > 0) then
                this%big = this%big - 1
                res = .true.
            else
                res = .false.
            end if
        case (2)
            if (this%medium > 0) then
                this%medium = this%medium - 1
                res = .true.
            else
                res = .false.
            end if
        case (3)
            if (this%small > 0) then
                this%small = this%small - 1
                res = .true.
            else
                res = .false.
            end if
        case default
            error stop 'Invalid car type'
    end select
end function addCar

end module ParkingSystem_mod
Compiled
Executed
Correct
!include "parking_system.f90"

program main
    implicit none

    integer, parameter :: big = 1, medium = 2, small = 3
    integer :: big_slots, medium_slots, small_slots
    integer :: car_type
    type(ParkingSystem) :: parking_system

    ! read the number of slots for each parking space
    read(*,*) big_slots, medium_slots, small_slots

    ! create a new parking system object
    parking_system = ParkingSystem(big_slots, medium_slots, small_slots)

    ! test the addCar function with different car types
    do car_type = 1, 3
        if (parking_system%addCar(car_type)) then
            write(*,*) "true"
        else
            write(*,*) "false"
        end if
    end do

end program

!------------------------------------------------------------------------------

module ParkingSystemModule
    implicit none

    type ParkingSystem
        integer :: big_slots, medium_slots, small_slots
        integer :: big_count, medium_count, small_count
    contains
        procedure :: addCar
    end type ParkingSystem

contains

    logical function addCar(this, carType) result(success)
        class(ParkingSystem), intent(inout) :: this
        integer, intent(in) :: carType

        success = .false.

        ! check if there is a parking space of carType available
        if (carType == 1) then
            if (this%big_count < this%big_slots) then
                this%big_count = this%big_count + 1
                success = .true.
            end if
        else if (carType == 2) then
            if (this%medium_count < this%medium_slots) then
                this%medium_count = this%medium_count + 1
                success = .true.
            end if
        else if (carType == 3) then
            if (this%small_count < this%small_slots) then
                this%small_count = this%small_count + 1
                success = .true.
            end if
        end if

    end function addCar

end module ParkingSystemModule
🌐 Data from online sources
def runningSum(nums):
    for i in range(1, len(nums)):
        nums[i] += nums[i - 1]
    return nums

The algorithm iterates over the input array starting from the second element (i = 1). In each iteration, it adds the (i-1)-th element to the i-th element of the array (nums[i] += nums[i-1]).

This operation computes the running sum of the array by essentially summing up every previous element with the current element. This is an in-place operation, and the modified input array is returned as output.

The time complexity of this algorithm is O(n), where n is the length of the input array, as the loop iterates once over each of the n elements in the array. The space complexity is O(1), since it only modifies the input array in-place and requires no additional memory.

🌐 Data from online sources
vector<int> runningSum(vector<int>& nums) {
    for (int i = 1; i < nums.size(); i++) {
        nums[i] += nums[i - 1];
    }
    return nums;
}

The algorithm iterates over the input array starting from the second element (i = 1). In each iteration, it adds the (i-1)-th element to the i-th element of the array (nums[i] += nums[i-1]).

This operation computes the running sum of the array by essentially summing up every previous element with the current element. This is an in-place operation, and the modified input array is returned as output.

The time complexity of this algorithm is O(n), where n is the length of the input array, as the loop iterates once over each of the n elements in the array. The space complexity is O(1), since it only modifies the input array in-place and requires no additional memory.