This is one of those problems where the clean idea matters more than the amount of code. For 1143. Longest Common Subsequence, the solution in this repository is mainly a dynamic programming 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: dynamic programming.
The notes already sitting in the source point us in the right direction:
- DP
- time: O(nm), space: O(nm)
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are longestCommonSubsequence.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- 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(nm), space: O(min(m,n))
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//DP
02//Runtime: 20 ms, faster than 27.49% of C++ online submissions for Longest Common Subsequence.
03//Memory Usage: 14.2 MB, less than 100.00% of C++ online submissions for Longest Common Subsequence.
04//time: O(nm), space: O(nm)
05class Solution {
06public:
07 int longestCommonSubsequence(string text1, string text2) {
08 int m = text1.size(), n = text2.size();
09 vector<vector<int>> dp(m, vector<int>(n, 0));
10
11 int i1 = 0, i2 = 0;
12
13 for(int i1 = 0; i1 < m; i1++){
14 for(int i2 = 0; i2 < n; i2++){
15 //distinguish the case whether text1[i1] == text2[i2] or not so the same char won't be counted twice
16 if(text1[i1] == text2[i2]){
17 dp[i1][i2] = ((i1 > 0 && i2 > 0) ? dp[i1-1][i2-1] : 0) + 1;
18 }else{
19 dp[i1][i2] = max(((i1 > 0) ? dp[i1-1][i2] : 0),
20 ((i2 > 0) ? dp[i1][i2-1] : 0));
21 }
22 // cout << i1 << " " << i2 << " " << dp[i1][i2] << endl;
23 }
24 }
25
26 return dp[m-1][n-1];
27 }
28};
29
30//DP, memory optimization
31//https://leetcode.com/problems/longest-common-subsequence/discuss/348884/C%2B%2B-with-picture-O(nm)
32//Runtime: 12 ms, faster than 87.59% of C++ online submissions for Longest Common Subsequence.
33//Memory Usage: 8 MB, less than 100.00% of C++ online submissions for Longest Common Subsequence.
34//time: O(nm), space: O(min(m,n))
35class Solution {
36public:
37 int longestCommonSubsequence(string text1, string text2) {
38 int m = text1.size(), n = text2.size();
39 vector<vector<int>> dp(2, vector<int>(n, 0));
40
41 int i1 = 0, i2 = 0;
42
43 for(int i1 = 0; i1 < m; i1++){
44 for(int i2 = 0; i2 < n; i2++){
45 //distinguish the case whether text1[i1] == text2[i2] or not so the same char won't be counted twice
46 if(text1[i1] == text2[i2]){
47 dp[i1%2][i2] = ((i1 > 0 && i2 > 0) ? dp[(i1-1)%2][i2-1] : 0) + 1;
48 }else{
49 dp[i1%2][i2] = max(((i1 > 0) ? dp[(i1-1)%2][i2] : 0),
50 ((i2 > 0) ? dp[i1%2][i2-1] : 0));
51 }
52 // cout << i1 << " " << i2 << " " << dp[i1][i2] << endl;
53 }
54 }
55
56 return dp[(m-1)%2][n-1];
57 }
58};
59
60//DP, further memory optimization
61//Runtime: 20 ms, faster than 75.55% of C++ online submissions for Longest Common Subsequence.
62//Memory Usage: 6.7 MB, less than 100.00% of C++ online submissions for Longest Common Subsequence.
63//time: O(nm), space: O(n)
64class Solution {
65public:
66 int longestCommonSubsequence(string text1, string text2){
67 int m = text1.size();
68 int n = text2.size();
69
70 vector<int> dp(n+1, 0);
71
72 for(int i = 1; i <= m; i++){
73 int dp_is1_js1 = dp[0]; //will be used when j = 1
74 // cout << dp[0] << " "; //0
75 for(int j = 1; j <= n; j++){
76 int dp_is1_j = dp[j];
77 if(text1[i-1] == text2[j-1]){
78 dp[j] = dp_is1_js1 + 1;
79 }else{
80 dp[j] = max(dp[j-1], dp[j]);
81 }
82 dp_is1_js1 = dp_is1_j;
83 }
84 }
85
86 return dp[n];
87 }
88};
89
90//recursion
91//https://leetcode.com/articles/delete-operation-for-two-strings/
92//TLE
93//time: O(2^max(m,n)), space: O(max(m,n))
94class Solution {
95public:
96 int longestCommonSubsequence(string text1, string text2, int m = -1, int n = -1) {
97 if(m == -1) m = text1.size();
98 if(n == -1) n = text2.size();
99
100 if(m == 0 || n == 0){
101 return 0;
102 }
103
104 if(text1[m-1] == text2[n-1]){
105 return 1 + longestCommonSubsequence(text1, text2, m-1, n-1);
106 }
107
108 return max(longestCommonSubsequence(text1, text2, m-1, n),
109 longestCommonSubsequence(text1, text2, m, n-1)
110 );
111 }
112};
113
114//recursion + memo
115//https://leetcode.com/articles/delete-operation-for-two-strings/
116//TLE
117//37 / 38 test cases passed.
118//time: O(m*n), space: O(m*n)
119class Solution {
120public:
121 vector<vector<int>> memo;
122
123 int longestCommonSubsequence(string text1, string text2, int m, int n) {
124 if(m == 0 || n == 0){
125 return 0;
126 }
127
128 if(memo[m][n] != -1) return memo[m][n];
129
130 if(text1[m-1] == text2[n-1]){
131 memo[m][n] = 1 + longestCommonSubsequence(text1, text2, m-1, n-1);
132 }else{
133 memo[m][n] = max(longestCommonSubsequence(text1, text2, m-1, n),
134 longestCommonSubsequence(text1, text2, m, n-1));
135 }
136
137 return memo[m][n];
138 }
139
140 int longestCommonSubsequence(string text1, string text2){
141 int m = text1.size(), n = text2.size();
142 memo = vector<vector<int>>(m+1, vector(n+1, -1));
143 return longestCommonSubsequence(text1, text2, m, n);
144 }
145};
Cost