We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a058fb4 commit 518d495Copy full SHA for 518d495
kotlin/1406-stone-game-iii.kt
@@ -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
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