File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun longestCommonSubsequence (text1 : String , text2 : String ): Int {
3+ if (text1.isEmpty() || text2.isEmpty()) {
4+ return 0
5+ }
6+
7+ val M = text1.length
8+ val N = text2.length
9+
10+ val dp = Array (M + 1 ){IntArray (N + 1 ){0 }}
11+
12+ for (i in 1 .. M ) {
13+ for (j in 1 .. N ) {
14+ if (text1[i - 1 ] == text2[j - 1 ]) {
15+ dp[i][j] = dp[i - 1 ][j - 1 ] + 1
16+ } else {
17+ dp[i][j] = Math .max(dp[i - 1 ][j], dp[i][j - 1 ])
18+ }
19+ }
20+ }
21+ return dp[M ][N ]
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun uniquePaths (m : Int , n : Int ): Int {
3+ val dp = Array (m + 1 ) { IntArray (n + 1 ) { 0 } }
4+ val indexM = m - 1
5+ val indexN = n - 1
6+ for (i in indexM downTo 0 ) {
7+ for (j in indexN downTo 0 ) {
8+ if (i == indexM && j == indexN) {
9+ dp[i][j] = 1
10+ } else {
11+ dp[i][j] = dp[i + 1 ][j] + dp[i][j + 1 ]
12+ }
13+ }
14+ }
15+ return dp[0 ][0 ]
16+ }
17+ }
You can’t perform that action at this time.
0 commit comments