← Home

1396. Design Underground System

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
data structure designC++Markdown
139

The trick here is to name the state correctly, then let the implementation follow. For 1396. Design Underground System, the solution in this repository is mainly a data structure design solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: data structure design.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are checkIn, checkOut, getAverageTime.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 344 ms, faster than 68.80% of C++ online submissions for Design Underground System.
02//Memory Usage: 57.5 MB, less than 100.00% of C++ online submissions for Design Underground System.
03class UndergroundSystem {
04public:
05    //(start, end) : (sum, count)
06    map<pair<string, string>, pair<int, int>> travels;
07    //int : stationName, time
08    map<int, pair<string, int>> persons;
09    
10    UndergroundSystem() {
11        
12    }
13    
14    void checkIn(int id, string stationName, int t) {
15        persons[id] = make_pair(stationName, t);
16    }
17    
18    void checkOut(int id, string stationName, int t) {
19        if(persons.find(id) != persons.end()){
20            string start = persons[id].first;
21            int startTime = persons[id].second;
22            int sum = 0, count = 0;
23            pair<string, string> travel = make_pair(persons[id].first, stationName);
24            if(travels.find(travel) != travels.end()){
25                sum += travels[travel].first;
26                count += travels[travel].second;
27            }
28            sum += (t - startTime);
29            count += 1;
30            travels[travel] = make_pair(sum, count);
31            persons.erase(id);
32        }
33    }
34    
35    double getAverageTime(string startStation, string endStation) {
36        pair<int, int> p = travels[make_pair(startStation, endStation)];
37        int sum = p.first, count = p.second;
38        return (double)sum/count;
39    }
40};
41
42/**
43 * Your UndergroundSystem object will be instantiated and called as such:
44 * UndergroundSystem* obj = new UndergroundSystem();
45 * obj->checkIn(id,stationName,t);
46 * obj->checkOut(id,stationName,t);
47 * double param_3 = obj->getAverageTime(startStation,endStation);
48 */

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.