Similar Problems

Similar Problems not available

Human Traffic Of Stadium - Leetcode Solution

Companies:

LeetCode:  Human Traffic Of Stadium Leetcode Solution

Difficulty: Hard

Topics: database  

Problem Statement: Design a system to manage the human traffic of a stadium, which controls the entrance and exit of people, and calculates the current amount of people inside the stadium at any time.

Solution: The given problem can be solved using object-oriented programming principles. We can design classes to represent the stadium and people, and use a queue to manage the flow of people entering and exiting the stadium.

Class Definitions:

  1. Stadium: This class represents a stadium and has many properties to track the state of the stadium. The properties and methods of this class are:

Properties:

  • max_capacity: A number representing the maximum capacity of the stadium.
  • current_capacity: A number representing the current number of people in the stadium.
  • entrance_queue: A queue to manage the flow of people entering the stadium.
  • exit_queue: A queue to manage the flow of people exiting the stadium.

Methods:

  • init: A constructor method to initialize the max_capacity, current_capacity, entrance_queue, and exit_queue properties.
  • admit: A method that accepts a person object and adds them to the entrance_queue if the stadium is not at max capacity. If the stadium is at max capacity, the person is denied entry.
  • exit: A method that removes a person from the stadium and adds them to the exit_queue if they are in the stadium.
  • get_current_capacity: A method that returns the current number of people inside the stadium.
  • get_max_capacity: A method that returns the maximum capacity of the stadium.
  1. Person: This class represents a person and has a name property.

Properties:

  • name: A string representing the name of the person.

Methods:

  • init: A constructor method to initialize the name property.

Algorithm:

  1. Create a stadium object with a set maximum capacity.
  2. Create person objects and use the admit method of the stadium object to allow them to enter the stadium.
  3. Use the exit method of the stadium object to allow people to leave the stadium.
  4. Use the get_current_capacity method of the stadium object to get the current number of people in the stadium.

Code:

class Stadium:
    def __init__(self, max_capacity):
        self.max_capacity = max_capacity
        self.current_capacity = 0
        self.entrance_queue = []
        self.exit_queue = []

    def admit(self, person):
        if self.current_capacity < self.max_capacity:
            self.current_capacity += 1
            self.entrance_queue.append(person)
        else:
            print(f"{person.name} cannot enter the stadium as it is at maximum capacity.")

    def exit(self, person):
        if self.current_capacity > 0:
            if person in self.entrance_queue:
                self.entrance_queue.remove(person)
                self.current_capacity -= 1
                self.exit_queue.append(person)
            elif person in self.exit_queue:
                print(f"{person.name} has already exited the stadium.")
            else:
                print(f"{person.name} is not in the stadium.")
        else:
            print("There are no people inside the stadium.")

    def get_current_capacity(self):
        return self.current_capacity

    def get_max_capacity(self):
        return self.max_capacity


class Person:
    def __init__(self, name):
        self.name = name

Example Usage:

# Create a stadium with max capacity of 10
stadium = Stadium(10)

# Create person objects
person1 = Person("John")
person2 = Person("Mary")
person3 = Person("Bob")
person4 = Person("Ann")

# Admit people to the stadium
stadium.admit(person1)  # current capacity: 1
stadium.admit(person2)  # current capacity: 2
stadium.admit(person3)  # current capacity: 3
stadium.admit(person4)  # current capacity: 4

# Exit people from the stadium
stadium.exit(person2)  # current capacity: 3
stadium.exit(person1)  # current capacity: 2

# Get current capacity of the stadium
print(stadium.get_current_capacity())  # Output: 2

Human Traffic Of Stadium Solution Code

1