File tree
Expand file treeCollapse file tree1 file changed
+5
-8
lines changed pygorithm/dynamic_programming
Expand file treeCollapse file tree1 file changed
+5
-8
lines changed Original file line number | Diff line number | Diff line change |
---|
@@ -28,19 +28,16 @@ def longest_common_subsequence(s1, s2):
|
28 | 28 | :param s2: string
|
29 | 29 | :return: int
|
30 | 30 | """
|
31 |
| -m = len(s1) |
32 |
| -n = len(s2) |
| 31 | +m, n = len(s1), len(s2) |
33 | 32 |
|
34 |
| -dp = [[0] * (n + 1) for i in range(m + 1)] |
| 33 | +dp = [[0] * (n + 1)] * (m + 1) |
35 | 34 | """
|
36 | 35 | dp[i][j] : contains length of LCS of s1[0..i-1] and s2[0..j-1]
|
37 | 36 | """
|
38 | 37 |
|
39 |
| -for i in range(m + 1): |
40 |
| -for j in range(n + 1): |
41 |
| -if i == 0 or j == 0: |
42 |
| -dp[i][j] = 0 |
43 |
| -elif s1[i - 1] == s2[j - 1]: |
| 38 | +for i in range(1, m + 1): |
| 39 | +for j in range(1, n + 1): |
| 40 | +if s1[i - 1] == s2[j - 1]: |
44 | 41 | dp[i][j] = dp[i - 1][j - 1] + 1
|
45 | 42 | else:
|
46 | 43 | dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
|
|
You can’t perform that action at this time.
0 commit comments