SW Test/Programmers

[Programmers] Lv2.์นดํŽซ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 9. 14. 02:17
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42842

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/42842

 

 

 

ํ’€์ด

  • ์™„์ „ํƒ์ƒ‰ ๋ฐฉ์‹์œผ๋กœ yellow ๊ธธ์ด์˜ ๋ฃจํŠธ๋งŒํผ๋งŒ ๋ฐ˜๋ณตํ•ด์„œ ๊ฐ€๋กœ, ์„ธ๋กœ ๊ธธ์ด๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ธธ์ด์— ๊ฐ๊ฐ +2์”ฉ ๋”ํ•œ ํ›„ ์‚ฌ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๊ณ  brown๊ณผ yellow์˜ ๊ฐฏ์ˆ˜์˜ ํ•ฉ๊ณผ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
import kotlin.math.*

class Solution {
    fun solution(brown: Int, yellow: Int): IntArray {
        (1..sqrt(yellow.toDouble()).toInt()).forEach{
            if(yellow % it == 0){
                val x = it + 2 
                val y = yellow / it + 2
                
                if(x*y == brown + yellow){
                    return intArrayOf(y, x)
                }
            }
        }
        return intArrayOf()
    }
}

 

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
๋ฐ˜์‘ํ˜•