Skip to content

Commit 518d495

Browse files
authored
Create 1406-stone-game-iii.kt
1 parent a058fb4 commit 518d495

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

kotlin/1406-stone-game-iii.kt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
fun stoneGameIII(s: IntArray): String {
3+
val dp = IntArray(s.size) { Integer.MIN_VALUE }
4+
5+
fun dfs(i: Int): Int {
6+
if (i == s.size)
7+
return 0
8+
if (dp[i] != Integer.MIN_VALUE)
9+
return dp[i]
10+
11+
var total = 0
12+
for (j in i until minOf(i + 3, s.size)) {
13+
total += s[j]
14+
dp[i] = maxOf(dp[i], total - dfs(j + 1))
15+
}
16+
17+
return dp[i]
18+
}
19+
20+
val sum = dfs(0)
21+
return if (sum > 0) "Alice" else if (sum < 0) "Bob" else "Tie"
22+
}
23+
}

0 commit comments

Comments
 (0)