Skip to content

Commit 595d698

Browse files
committed
Added 62-Unique-Paths.kt and 1143-Longest-Common-Subsequence.kt
1 parent 8a8654d commit 595d698

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

kotlin/62-Unique-Paths.kt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)