๊ธ€ ์ž‘์„ฑ์ž: ํƒ์‹œ ์šด์ „์‚ฌ
๋ฐ˜์‘ํ˜•
--- ---

๐ŸŽจ ์นดํŽซ ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ


๐Ÿ˜ƒ ๋‚˜์˜ ์ฝ”๋“œ

def solution(brown, red):
    for a in range(1, int(red**0.5)+1):
        if not red % a:
            b = red // a
            if 2*a + 2*b + 4 == brown:
                return [b+2, a+2]

์™„์ „ ํƒ์ƒ‰(exhaustive search)๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ๋นจ๊ฐ„์ƒ‰์˜ ๊ฐ€๋กœ๋ฅผ A, ์„ธ๋กœ๋ฅผ B๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ์ฃผ์–ด์ง„ ๊ฐ’ red๋Š” A*B๊ฐ€ ๋˜๊ณ  brown์€ 2(A+B)+4๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด์ œ A์™€ B์— ์ฐพ๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. red๋ฅผ ๋‘ ์ž์—ฐ์ˆ˜ ๊ณฑ์˜ ์Œ์œผ๋กœ ๋ดค์„ ๋•Œ, ์ค‘๋ณต๋œ ๊ณฑ์˜ ์Œ์„ ์—†์—”๋‹ค๋ฉด A์˜ ๋ฒ”์œ„๋Š” red์˜ ์ œ๊ณฑ๊ทผ์˜ ๋‚ด๋ฆผ๊ฐ’๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์–ป์€ ์ž์—ฐ์ˆ˜ A์™€ B์˜ ์Œ์ด brown์— ๊ฐ’์—๋„ ๋งž๋Š” ์ง€ ํ™•์ธํ•˜๋ฉด ๋ฌธ์ œ๋Š” ๋งˆ๋ฌด๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

Written with StackEdit.


๋ฐ˜์‘ํ˜•