๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

 

๋ฌธ์ œ ํ’€์ด

์ฒ˜์Œ ์ ‘๊ทผ๋ฒ•์€ ๊ธธ์ด๊ฐ€ 100์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  n๊ฐœ ์ด์ƒ์˜ ๋žœ์„ ์ด ๋‚˜์˜ค๋Š”์ง€ ์ฒดํฌ,

100 -> 200 -> 300 ๋Š˜๋ ค๊ฐ€๋ฉฐ ์ƒ๊ฐํ•˜๋‹ค ์ด์ง„ํƒ์ƒ‰์„ ๋– ์˜ฌ๋ฆฌ๊ฒŒ ๋˜์—ˆ๋‹ค. (์™„์ „ํƒ์ƒ‰๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—)

 

left๋Š” 1, right๋Š” ๊ฐ€์ง„๋žœ์„ ์˜ ์ตœ๋Œ€๊ธธ์ด๋กœ ๋‘์—ˆ๋‹ค.

๊ธฐ๋ณธ์ ์ธ ์ด์ง„ํƒ์ƒ‰ ํ’€์ด๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ!

 

 

์ฝ”๋“œ

import Foundation

let input = readLine()!.split(separator: " ").map {Int(String($0))!}
let k = input[0], n = input[1]
var arrs = [Int]()
for _ in 0..<k {
    arrs.append(Int(readLine()!)!)
}

var left = 1
var right = arrs.max()!
var answer = 0
while left <= right {
    
    let mid = (left+right) / 2
    var sum = 0
    for arr in arrs {
        sum += arr/mid
    }

    if sum >= n {
        answer = mid // n๊ฐœ์ด์ƒ ๋žœ์„ ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ answer์— ์ €์žฅ
        left = mid + 1
    } else {
        right = mid - 1
    }
}

print(answer)

 

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...