Leetcode 1396: Design Underground System

grid47
grid47
Exploring patterns and algorithms
Jun 20, 2024 5 min read

You are tasked with building a system to track customer travel times within an underground railway network. The system should calculate the average time taken to travel between two stations based on previous trips. Implement the UndergroundSystem class with the following methods.
Problem
Approach
Steps
Complexity
Input: The input consists of several commands, each one representing a method call to the UndergroundSystem class. Each command is represented as an array where the first element is the method name, and the rest are its respective arguments.
Example: ["UndergroundSystem", "checkIn", "checkOut", "getAverageTime"]
Constraints:
• 1 <= id, t <= 10^6
• 1 <= stationName.length <= 10
• All strings consist of uppercase and lowercase English letters and digits.
Output: The output for each method call will be either null (for checkIn and checkOut methods) or a double representing the average time for the given trip (for getAverageTime).
Example: [null, null, 10.00000]
Constraints:
• Answers within 10^-5 of the actual value will be accepted.
Goal: To efficiently track and calculate the average travel times between stations.
Steps:
• 1. Use a map to store the check-in times and stations for each customer.
• 2. When a customer checks out, calculate the travel time and update the sum and count of trips between stations.
• 3. To calculate the average time between two stations, divide the total time by the number of trips between the stations.
Goal: Ensure that all method calls and inputs are consistent with the problem statement's restrictions.
Steps:
• There will be at least one call to getAverageTime before it is called.
Assumptions:
• All method calls will be valid and in chronological order.
• No customer will check in or check out at the same time.
Input: Input: ["UndergroundSystem", "checkIn", "checkOut", "getAverageTime"]
Explanation: In this example, we simulate customers checking in and out of stations and calculating average travel times.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus