This problem looks busy at first, but the accepted solution is built around one steady invariant. For 72. Edit Distance, the solution in this repository is mainly a dynamic programming 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: dynamic programming.
The notes already sitting in the source point us in the right direction:
- DP
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 minDistance.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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(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
01//DP
02//Runtime: 32 ms, faster than 17.80% of C++ online submissions for Edit Distance.
03//Memory Usage: 8.9 MB, less than 87.50% of C++ online submissions for Edit Distance.
04class Solution {
05public:
06 int minDistance(string word1, string word2) {
07 int m = word1.size();
08 int n = word2.size();
09
10 /*
11 different from other problem,
12 here dp[0][?] and dp[?][0] are meaningful.
13 They represents the situation when word1 is "" and when word2 is ""
14 */
15 vector<vector<int>> dp(m+1, vector(n+1, 0));
16
17 for(int i = 0; i <= m; i++){
18 for(int j = 0; j <= n; j++){
19 if(i == 0 && j == 0){
20 dp[i][j] = 0;
21 }else if(j == 0){
22 /*
23 word1: [0, i-1] //i 's upper limit is m
24 word2: ""
25
26 this means we need to delete i char from word1
27 */
28 dp[i][j] = i;
29 }else if(i == 0){
30 /*
31 word1: ""
32 word2: [0, j-1] //j 's upper limit is n
33 */
34 dp[i][j] = j;
35 }else if(word1[i-1] == word2[j-1]){
36 //no op
37 dp[i][j] = dp[i-1][j-1];
38 }else{
39 dp[i][j] = min({
40 //remove word1[i-1]
41 dp[i-1][j],
42 //insert word2[j-1]
43 dp[i][j-1],
44 //replace word1[i-1] to word2[j-1]
45 dp[i-1][j-1]
46 }) + 1;
47 }
48 }
49 }
50
51 return dp[m][n];
52 }
53};
54
55//DP, O(1) space
56//https://leetcode.com/problems/edit-distance/discuss/25846/C%2B%2B-O(n)-space-DP
57//Runtime: 16 ms, faster than 61.68% of C++ online submissions for Edit Distance.
58//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for Edit Distance.
59class Solution {
60public:
61 int minDistance(string word1, string word2) {
62 int m = word1.size();
63 int n = word2.size();
64
65 vector<int> dp(n+1, 0);
66 int dp_is1_js1;
67
68 for(int i = 0; i <= m; i++){
69 for(int j = 0; j <= n; j++){
70 int tmp = dp[j];
71 if(i == 0 && j == 0){
72 dp[j] = 0;
73 }else if(j == 0){
74 /*
75 word1: [0, i-1] //i 's upper limit is m
76 word2: ""
77
78 this means we need to delete i char from word1
79 */
80 dp[j] = i;
81 }else if(i == 0){
82 /*
83 word1: ""
84 word2: [0, j-1] //j 's upper limit is n
85 */
86 dp[j] = j;
87 }else if(word1[i-1] == word2[j-1]){
88 //no op
89 dp[j] = dp_is1_js1;
90 }else{
91 dp[j] = min({
92 //remove word1[i-1]
93 //dp[i-1][j]
94 dp[j],
95 //insert word2[j-1]
96 //dp[i][j-1]
97 dp[j-1],
98 //replace word1[i-1] to word2[j-1]
99 //dp[i-1][j-1]
100 dp_is1_js1
101 }) + 1;
102 }
103 dp_is1_js1 = tmp;
104 }
105 }
106
107 return dp[n];
108 }
109};
Cost