-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
2140-solving-questions-with-brainpower.kt
62 lines (52 loc) · 1.28 KB
/
2140-solving-questions-with-brainpower.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* DFS/Recursion + Memoization
*/
class Solution {
fun mostPoints(questions: Array<IntArray>): Long {
val memo = LongArray(questions.size) { -1L }
fun dfs(i: Int): Long {
if (i >= questions.size)
return 0
if (memo[i] != -1L)
return memo[i]
memo[i] = maxOf(
questions[i][0] + dfs(i + 1 + questions[i][1]),
dfs(i + 1)
)
return memo[i]
}
return dfs(0)
}
}
/*
* DP
*/
class Solution {
fun mostPoints(q: Array<IntArray>): Long {
val n = q.lastIndex
val dp = LongArray(q.size)
for (i in n downTo 0) {
dp[i] = maxOf(
if (i < n) dp[i + 1] else 0,
q[i][0] + if (i + 1 + q[i][1] <= n) dp[i + 1 + q[i][1]] else 0
)
}
return dp[0]
}
}
/*
* DP with HashMap instead of Array for simpler code
*/
class Solution {
fun mostPoints(q: Array<IntArray>): Long {
val n = q.lastIndex
val dp = HashMap<Int, Long>()
for (i in n downTo 0) {
dp[i] = maxOf(
dp[i + 1] ?: 0L,
q[i][0] + (dp[i + 1 + q[i][1]] ?: 0L)
)
}
return dp[0] ?: 0L
}
}