← Home

1035. Uncrossed Lines

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
103

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1035. Uncrossed Lines, the solution in this repository is mainly a dynamic programming 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: dynamic programming.

The notes already sitting in the source point us in the right direction:

  • LCS, DP, O(N^2) space
  • time: O(N^2), space: O(N^2)

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 maxUncrossedLines.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • 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. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

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^2), space: O(N)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//LCS, DP, O(N^2) space
02//Runtime: 40 ms, faster than 14.05% of C++ online submissions for Uncrossed Lines.
03//Memory Usage: 13.2 MB, less than 9.09% of C++ online submissions for Uncrossed Lines.
04//time: O(N^2), space: O(N^2)
05class Solution {
06public:
07    int maxUncrossedLines(vector<int>& A, vector<int>& B) {
08        int m = A.size();
09        int n = B.size();
10        
11        vector<vector<int>> dp(m+1, vector(n+1, 0));
12        
13        for(int i = 1; i <= m; i++){
14            for(int j = 1; j <= n; j++){
15                if(A[i-1] == B[j-1]){
16                    dp[i][j] = dp[i-1][j-1] + 1;
17                }else{
18                    dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
19                }
20            }
21        }
22        
23        return dp[m][n];
24    }
25};
26
27//LCS, DP, O(N) space
28//Runtime: 12 ms, faster than 78.56% of C++ online submissions for Uncrossed Lines.
29//Memory Usage: 9.8 MB, less than 77.27% of C++ online submissions for Uncrossed Lines.
30//time: O(N^2), space: O(N)
31class Solution {
32public:
33    int maxUncrossedLines(vector<int>& A, vector<int>& B) {
34        int m = A.size();
35        int n = B.size();
36        
37        vector<int> dp(n+1, 0);
38        
39        for(int i = 1; i <= m; i++){
40            int dp_is1_js1 = dp[0]; //will be used when j = 1
41            // cout << dp[0] << " "; //0
42            for(int j = 1; j <= n; j++){
43                int dp_is1_j = dp[j];
44                if(A[i-1] == B[j-1]){
45                    dp[j] = dp_is1_js1 + 1;
46                }else{
47                    dp[j] = max(dp[j-1], dp[j]);
48                }
49                dp_is1_js1 = dp_is1_j;
50            }
51        }
52        
53        return dp[n];
54    }
55};

Cost

Complexity

Time
O(N^2), space: O(N)
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.