[백준 14754 - Kotlin] Pizza Boxes

kldaji·2022년 4월 1일
1

백준

목록 보기
49/76
post-custom-banner

문제링크

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, m)
    val (n, m) = bufferedReader
        .readLine()
        .split(" ")
        .map { it.toInt() }

    // 2. get boxes
    val boxes = mutableListOf<List<Int>>()
    for (i in 0 until n) {
        boxes.add(bufferedReader.readLine().split(" ").map { it.toInt() })
    }

    // 3. get Max Height
    val isMaxHeight = Array(n) { Array(m) { false } }
    for (i in 0 until m) {
        var maxHeightIndex = 0
        for (j in 0 until n) {
            if (boxes[j][i] > boxes[maxHeightIndex][i]) {
                maxHeightIndex = j
            }
        }
        isMaxHeight[maxHeightIndex][i] = true
    }
    for (i in 0 until n) {
        var maxHeightIndex = 0
        for (j in 0 until m) {
            if (boxes[i][j] > boxes[i][maxHeightIndex]) {
                maxHeightIndex = j
            }
        }
        isMaxHeight[i][maxHeightIndex] = true
    }

    // 4. remove box
    var removedBoxes = 0L
    for (i in 0 until n) {
        for (j in 0 until m) {
            if (!isMaxHeight[i][j]) {
                removedBoxes += boxes[i][j]
            }
        }
    }

    bufferedWriter.write("$removedBoxes")

    bufferedReader.close()
    bufferedWriter.close()
}
profile
다양한 관점에서 다양한 방법으로 문제 해결을 지향하는 안드로이드 개발자 입니다.
post-custom-banner

0개의 댓글