[伯俊19941-Kotlin]分配ハンバーガー


質問リンク
import java.io.BufferedReader
import java.io.BufferedWriter

private lateinit var bufferedReader: BufferedReader
private lateinit var bufferedWriter: BufferedWriter

fun main() {
    bufferedReader = System.`in`.bufferedReader()
    bufferedWriter = System.out.bufferedWriter()

    // 1. get (n, k)
    val (n, k) = bufferedReader
        .readLine()
        .split(" ")
        .map { it.toInt() }

    // 2. get hp
    val hp = bufferedReader.readLine()
        .split("")
        .filter { it.isNotBlank() }

    val people = mutableListOf<Int>()
    val visited = Array(n) { false }
    for (i in hp.indices) {
        if (hp[i] == "P") {
            people.add(i)
            visited[i] = true
        }
    }

    var answer = 0
    // 3. person index 순회
    for (personIndex in people) {
        for (j in (personIndex - k)..(personIndex + k)) {
            // 4. out of range
            if (j !in 0 until n) continue
            // 5. already eaten or person
            if (visited[j]) continue
            // 6. eat hamburger
            visited[j] = true
            answer++
            break
        }
    }

    bufferedWriter.write("$answer")

    bufferedReader.close()
    bufferedWriter.close()
}