본문 바로가기

독서/알고리즘

백준 1965 상자넣기

728x90
data class Matryoshka constructor(private val size: Int) {
    fun lessThen(other: Matryoshka): Boolean {
        return this.size < other.size
    }
}

class MatryoshkaLine constructor(private val nums: Array<Matryoshka>) {
    fun solve(): Int {
        val dp = Array(nums.size) { 0 }
        var result = 0
        for (i in nums.indices) {
            val l = getLongest(i, dp)
            dp[i] = l + 1;
            result = result.coerceAtLeast(dp[i])
        }
        return result
    }

    private fun getLongest(i: Int, dp: Array<Int>): Int {
        var l = 0
        for (j in 0 until i) {
            if (nums[j].lessThen(nums[i])) {
                l = l.coerceAtLeast(dp[j])
            }
        }
        return l
    }
}

fun main(args: Array<String>) = with(System.`in`.bufferedReader()) {
    readLine()
    val nums = readLine().trim().split(" ").map { Matryoshka(it.toInt()) }.toTypedArray()
    print(MatryoshkaLine(nums).solve())
}

 

'독서 > 알고리즘' 카테고리의 다른 글

친구  (0) 2023.12.31
연결 요소의 개수  (0) 2023.12.15
백준 2110 공유기 설치  (0) 2021.12.30
백준 23352 방탈출  (0) 2021.12.28
백준 1188 음식 평론가  (0) 2021.12.27