← Home

1496. Path Crossing

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

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

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isPathCrossing.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • 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: 4 ms, faster than 97.22% of C++ online submissions for Path Crossing.
02//Memory Usage: 7 MB, less than 100.00% of C++ online submissions for Path Crossing.
03class Solution {
04public:
05    bool isPathCrossing(string path) {
06        set<vector<int>> visited;
07        pair<set<vector<int>>::iterator, bool> res;
08        int i = 0, j = 0;
09        
10        visited.insert({0, 0});
11        
12        for(char c : path){
13            switch(c){
14                case 'N':
15                    res = visited.insert({--i, j});
16                    if(!res.second) return true;
17                    break;
18                case 'S':
19                    res = visited.insert({++i, j});
20                    if(!res.second) return true;
21                    break;
22                case 'W':
23                    res = visited.insert({i, --j});
24                    if(!res.second) return true;
25                    break;
26                case 'E':
27                    res = visited.insert({i, ++j});
28                    if(!res.second) return true;
29                    break;
30            }
31        }
32        
33        return false;
34    }
35};

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.