← Home

1436. Destination City

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
143

This is one of those problems where the clean idea matters more than the amount of code. For 1436. Destination City, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. 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: straightforward implementation.

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are destCity.

Guide

Why?

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

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • 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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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: 24 ms, faster than 75.00% of C++ online submissions for Destination City.
02//Memory Usage: 10.9 MB, less than 100.00% of C++ online submissions for Destination City.
03class Solution {
04public:
05    string destCity(vector<vector<string>>& paths) {
06        set<string> cities;
07        
08        for(vector<string>& path: paths){
09            cities.insert(path[1]);
10        }
11        
12        /*
13        need to do two-pass!!
14        cannot delete start point while adding ending point,
15        e.g. [["pYyNGfBYbm","wxAscRuzOl"],["kzwEQHfwce","pYyNGfBYbm"]]
16        "pYyNGfBYbm" will not be deleted in one-pass method
17        */
18        for(vector<string>& path: paths){
19            // auto it = cities.find(path[0]);
20            // if(it != cities.end()){
21            //     cities.erase(it);
22            // }
23            cities.erase(path[0]);
24        }
25        
26        return *cities.begin();
27    }
28};

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.