We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 562333a commit b6e7f66Copy full SHA for b6e7f66
kotlin/0646-maximum-length-of-pair-chain.kt
@@ -34,3 +34,31 @@ class Solution {
34
return dp[n - 1]
35
}
36
37
+
38
+//recursion + memoization O(n^2)
39
+class Solution {
40
+ fun findLongestChain(_pairs: Array<IntArray>): Int {
41
+ val pairs = _pairs.sortedWith(compareBy({ it[0] }, { it[1] }))
42
+ val dp = IntArray (pairs.size) { -1 }
43
44
+ fun dfs(i: Int): Int {
45
+ if (i == pairs.size) return 0
46
+ if (dp[i] != -1) return dp[i]
47
48
+ var res = 1
49
+ for (j in 0 until i) {
50
+ if (pairs[i][0] > pairs[j][1])
51
+ res = maxOf(res, dfs(j) + 1)
52
+ }
53
54
+ dp[i] = res
55
+ return res
56
57
58
59
+ for (i in 0 until pairs.size)
60
+ res = maxOf(res, dfs(i))
61
62
63
64
+}
0 commit comments