File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
longest-common-subsequence Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def longestCommonSubsequence (self , text1 : str , text2 : str ) -> int :
3+ dp = [0 ] * (len (text2 ) + 1 )
4+
5+ for i in range (len (text1 )):
6+ prev_row = dp [:]
7+ for j in range (len (text2 )):
8+ if text1 [i ] == text2 [j ]:
9+ dp [j + 1 ] = prev_row [j ] + 1
10+ else :
11+ dp [j + 1 ] = max (dp [j ], prev_row [j + 1 ])
12+
13+ return dp [- 1 ]
14+
15+ ## TC: O(mn), SC: O(n)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def uniquePaths (self , m : int , n : int ) -> int :
3+ dp = [1 ] * n
4+
5+ for _ in range (1 , m ):
6+ for i in range (1 , n ):
7+ dp [i ] += dp [i - 1 ]
8+
9+ return dp [- 1 ]
10+ ## TC: O(mn), SC: O(n)
11+
12+ ## return math.comb(m + n - 2, m - 1)
13+ ## TC: O(1) whatever constant, SC: O(1)
You can’t perform that action at this time.
0 commit comments