-
Notifications
You must be signed in to change notification settings - Fork 0
/
StoneGame.kt
37 lines (34 loc) · 893 Bytes
/
StoneGame.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
package problems
class StoneGame {
//But as the given rules state, alex will always win...!
fun stoneGame(piles: IntArray): Boolean {
var beginning = 0
var end = piles.size - 1
var alexTurn = true
var alex = 0
var lee = 0
while (beginning <= end) {
val b = piles[beginning]
val e = piles[end]
if (alexTurn) {
if (b < e) {
alex += e
end--
} else {
alex += b
beginning++
}
} else {
if (b < e) {
lee += b
beginning++
} else {
lee += e
end--
}
}
alexTurn = !alexTurn
}
return alex > lee
}
}