• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

프로그래머스 - 43238 입국심사

03 Jul 2022

Reading time ~1 minute

문제

43238 입국심사

screencapture

풀이

  1. 입국 심사에 걸리는 시간은 1 ~ 가장 긴 심사 시간 * 입국심사를 기다리는 사람 수이다.
  2. 이 시간을 기준으로 이진 검색을 한다.

답

kotlin code

class Solution {
    fun solution(n: Int, times: IntArray): Long = solution(1L, times.maxOf { it } * n.toLong(), n, times)

    tailrec fun solution(min: Long, max: Long, n: Int, times: IntArray): Long {
        if(min > max) return min
        val mid = (min + max) / 2
        val count = times.sumOf { mid / it }
        return if(count < n) solution(mid + 1, max, n, times) else solution(min, mid - 1, n, times)
    }
}


programmerskotlin코틀린프로그래머스Lv.3풀이 Share Tweet +1