-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
50 lines (44 loc) · 1.22 KB
/
solution.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
data class Balls(val count: Int, val isEnd: Boolean)
fun main(args: Array<out String>) {
readLine()
val redBalls = mutableListOf<Balls>()
val blueBalls = mutableListOf<Balls>()
var count = 0
var isLastBlue: Boolean? = null
var isBegin = true
loop@for (c in readLine()!!) {
val isCurrentBlue = c == 'B'
if (isLastBlue == null) {
isLastBlue = isCurrentBlue
}
when (isLastBlue) {
isCurrentBlue -> {
count += 1
continue@loop
}
true -> {
blueBalls += Balls(count, isBegin)
}
false -> {
redBalls += Balls(count, isBegin)
}
}
isBegin = false
count = 1
isLastBlue = isCurrentBlue
}
if (isLastBlue == true) {
blueBalls += Balls(count, true)
} else {
redBalls += Balls(count, true)
}
val answer = listOf(
redBalls.asSequence(),
blueBalls.asSequence(),
redBalls.reversed().asSequence(),
blueBalls.reversed().asSequence()
).map {
it.withIndex().sumBy { (i, v) -> if(i == 0 && v.isEnd) 0 else v.count }
}.min()
println(answer)
}