๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Ÿ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/๐Ÿ˜  | Level 3

[Kotlin, Lv.3] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ž…๊ตญ์‹ฌ์‚ฌ

by immgga 2024. 8. 7.

์ถœ์ฒ˜: unsplash.com

 

์ž…๊ตญ์‹ฌ์‚ฌ

Level 3

https://school.programmers.co.kr/learn/courses/30/lessons/43238?language=kotlin

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

์ด๋ถ„ ํƒ์ƒ‰์„ ์ด์šฉํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

์–ด๋””์„œ ์ด๋ถ„ ํƒ์ƒ‰์„ ์จ์•ผ ํ• ๊นŒ?

 

n๋ช…์„ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐ ์ด ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ด๋ถ„ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

์ด ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์„๊นŒ?

์˜ˆ์ œ 1์—์„œ๋Š” n = 6์ด๊ณ , ์‹ฌ์‚ฌ๊ด€์ด ๊ฒ€์‚ฌํ•˜๋Š” ๋ฐ ๋“œ๋Š” ์‹œ๊ฐ„ 10์ดˆ, 7์ดˆ์ด๊ณ  ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์ด 28์ดˆ์ด๋‹ค.

 

28์ดˆ๊ฐ€ ๋‚˜์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” 7์ดˆ ๊ฑธ๋ฆฌ๋Š” ์‹ฌ์‚ฌ๊ด€์ด 4๋ช…์„ ๊ฒ€์‚ฌํ•˜๊ณ , 10์ดˆ ๊ฑธ๋ฆฌ๋Š” ์‹ฌ์‚ฌ๊ด€์ด 2๋ช…์„ ๊ฒ€์‚ฌํ•˜๋ฉด ๋”ฑ 6๋ช…์ด ๋ผ์„œ ๊ฒ€์‚ฌ๋ฅผ ๋๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

์ด ์‚ฌ์‹ค๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณต์‹์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

28 / 4 + 28 / 10 = 6

๊ฑธ๋ฆฐ ์‹œ๊ฐ„์— ๊ฐ ์‹ฌ์‚ฌ๊ด€์˜ ๊ฒ€์‚ฌ์‹œ๊ฐ„์„ ๋‚˜๋ˆ„๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ฒ„๋ฆฐ ๊ฐ’์˜ ํ•ฉ์ด n๊ณผ ๊ฐ™์•„์•ผ ์ •ํ™•ํ•œ ์ด ๊ฒ€์‚ฌ์‹œ๊ฐ„์ด ๋‚˜์˜จ๋‹ค.

์ด๋ฅผ ์ด์šฉํ•ด ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋ณด๊ฒ ๋‹ค.

 

์ด๋ถ„ ํƒ์ƒ‰์—๋Š” ์‹œ์ž‘์ ๊ณผ ์ข…๋ฃŒ์ ์ด ์žˆ๋‹ค.

์‹œ์ž‘์ ์€ 0์œผ๋กœ ํ•˜๊ณ , ์ข…๋ฃŒ์ ์„ n๋ช…์ด ๊ฒ€์‚ฌ๋ฐ›์•˜์„ ๋•Œ, ๊ฐ€์žฅ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ๊ฒ€์‚ฌ ์‹œ๊ฐ„์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

๊ฐ€์žฅ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋ ค๋ฉด ๊ฐ€์žฅ ๊ฒ€์‚ฌ๊ฐ€ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ์‹ฌ์‚ฌ๊ด€์˜ ๊ฒ€์‚ฌ ์‹œ๊ฐ„์— n์„ ๊ณฑํ•˜๋ฉด ๋œ๋‹ค.

binarySearch(0, examiner.last() * people)

 

<์กฐ๊ฑด 1>

๋‹ค์Œ์œผ๋กœ ์ด๋ถ„ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์‹œ์ž‘์ ๊ณผ ์ข…๋ฃŒ์ ์„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค.

์‹œ์ž‘์ ์ด ๋ณ€๊ฒฝ๋˜๋ ค๋ฉด mid(ํ˜„์žฌ ๊ฒ€์‚ฌ ์‹œ๊ฐ„)๋ฅผ ์ด์šฉํ•ด ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์ด n๋ณด๋‹ค ์ ์€ ๊ฒฝ์šฐ์—๋Š” ์‹œ์ž‘์ ์„ mid + 1๋กœ ๋ณ€๊ฒฝํ•ด ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ขํž ์ˆ˜ ์žˆ๋‹ค.

val mid = (end + start) / 2L
val res = examiner.sumOf { mid / it }

if (res < people)

 

 

<์กฐ๊ฑด 2>

๋งŒ์•ฝ mid๋ฅผ ์ด์šฉํ•ด ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด n๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ข…๋ฃŒ์ ์„ ์ค„์—ฌ์„œ ๊ฒ€์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ขํ˜€์•ผ ํ•œ๋‹ค. ์ข…๋ฃŒ์ ์„ mid - 1๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ์–ด์ฐจํ”ผ mid ์ดํ›„์˜ ๊ฐ’๋“ค๋„ ๋ชจ๋‘ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด n๋ณด๋‹ค ํด ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

n๊ณผ mid๋กœ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜๊ฐ€ ๊ฐ™์•„์•ผ ์ •๋‹ต์ด ๋œ๋‹ค.

์ด ์กฐ๊ฑด์— ๋“ค์–ด์˜ค๋ฉด ์ •๋‹ต ํ›„๋ณด๊ฐ€ ๋˜์–ด answer์— ์ €์žฅ๋œ๋‹ค. ํ•˜์ง€๋งŒ answer๊ฐ€ ๋ฐ”๋€” ์ˆ˜๋„ ์žˆ๋‹ค.

else {
    answer = mid
    binarySearch(start, mid - 1)
}

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
private var examiner = listOf<Long>()
private var people = 0
private var answer: Long = 0

fun solution(n: Int, times: IntArray): Long {
    times.sort()
    examiner = times.map { it.toLong() }
    people = n

    binarySearch(0, examiner.last() * people)
    println(answer)

    return answer
}

private fun binarySearch(start: Long, end: Long) {
    if (start > end) return

    val mid = (end + start) / 2L
    val res = examiner.sumOf { mid / it }

    if (res < people) {
        binarySearch(mid + 1, end)
    } else {
        answer = mid
        binarySearch(start, mid - 1)
    }
}

 

๋ฌธ์ œ ํ’€์ด

times๋ฅผ ์ •๋ ฌํ•œ ๊ฐ’์„ examiner์— ์ €์žฅํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  binarySearch ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

binarySearch๋Š” end๊ฐ€ start๋ณด๋‹ค ์ž‘์•„์ง„๋‹ค๋ฉด ์ข…๋ฃŒ๋œ๋‹ค.

end์™€ start์˜ ์ค‘๊ฐ„๊ฐ’์ธ mid๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. mid๊ฐ€ ์ด ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์ด ๋  ๊ฒƒ์ด๋‹ค.

examiner์™€ mid๋ฅผ ์ด์šฉํ•ด mid๋ถ„ ๋™์•ˆ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์กฐ๊ฑด 1์„ ๋งŒ์กฑํ•œ๋‹ค๋ฉด start์˜ ๋ฒ”์œ„๋ฅผ mid + 1๋กœ ์„ค์ •ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์กฐ๊ฑด 2๋ฅผ ์ ์šฉํ•ด end๋ฅผ mid - 1๋กœ ์„ค์ •ํ•˜๊ณ  ์žฌ๊ท€ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

search๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๊ฒƒ์„ ์จ์•ผ ํ• ์ง€ ๊ฐ์ด ์žกํžˆ์ง€ ์•Š์•„์„œ ์ ‘๊ทผ๋ฒ•์„ ์ฐพ์•„์„œ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„๊ณผ ๊ฒ€์‚ฌ ์‹œ๊ฐ„์œผ๋กœ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์ด ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

728x90