Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ „ํƒ์ƒ‰ - ์นดํŽซ (level.2)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 23. 19:54
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ๋งํฌ

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์นดํŽซ

Leo๋Š” ์นดํŽซ์„ ์‚ฌ๋Ÿฌ ๊ฐ”๋‹ค๊ฐ€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ค‘์•™์—๋Š” ๋…ธ๋ž€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ณ  ํ…Œ๋‘๋ฆฌ 1์ค„์€ ๊ฐˆ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๋Š” ๊ฒฉ์ž ๋ชจ์–‘ ์นดํŽซ์„ ๋ดค์Šต๋‹ˆ๋‹ค. Leo๋Š” ์ง‘์œผ๋กœ ๋Œ์•„์™€์„œ ์•„๊นŒ ๋ณธ ์นดํŽซ์˜ ๋…ธ๋ž€์ƒ‰๊ณผ

programmers.co.kr

 

๋‚˜์˜ ํ’€์ด

์ผ๋‹จ brown๊ณผyellow์˜ ํ•ฉ์˜ ์•ฝ์ˆ˜๋งŒ ํ•œ๋ณ€์˜ ๊ธธ์ด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์บ์น˜ํ•ด์•ผํ–ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ , ๊ทœ์น™์„ ์‚ดํŽด๋ณด๋ฉด, ๋ฌธ์ œ์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด yellow๊ฐ€ brown์— ๋ชจ๋‘ ๋‘˜๋Ÿฌ์Œ“์—ฌ์žˆ์œผ๋ ค๋ฉด ๊ฐ ๋ณ€์˜ -2ํ•œ ๊ฒƒ๋“ค์˜ ๊ณฑ์ด yellow์˜ ๊ฐฏ์ˆ˜์™€ ๊ฐ™์•„์•ผํ•จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Œ€๋กœ ์ƒ๊ฐํžŒ๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.

def solution(brown, yellow):
    answer = [] 
    div = [] # ์•ฝ์ˆ˜๋ฅผ ๋„ฃ์„๊ฑฐ์ž„
    total = brown + yellow
    for i in range(3, total+1):
        if total % i == 0:
            div.append(i)
    for d in div:
        n = total //d
        if (d-2)*(n-2) == yellow :
            answer.append(n)
            answer.append(d)
            answer.sort(reverse = True)
            break
        else:
            pass
    return answer

 

๋‹ค๋ฅธ์‚ฌ๋žŒํ’€์ด

https://programmers.co.kr/learn/courses/30/lessons/42842/solution_groups?language=python3 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

def solution(brown, red):
    for i in range(1, int(red**(1/2))+1):
        if red % i == 0:
            if 2*(i + red//i) == brown-4:
                return [red//i+2, i+2]

์ฝ”๋“œ๊ฐ€ ์ƒ๋‹นํžˆ ๊ฐ„๊ฒฐํ•˜๋‹ค. ํ•˜์ง€๋งŒ ๋งˆ์ง€๋ง‰์— ๊ฒฐ๊ตญ +2์”ฉ ํ•ด๋†“์€ ๊ฒƒ์„ ๋ณด๋ฉด, ๋‚˜์™€ ๊ฐ™์€ ๊ทœ์น™์„ ์ฐพ์€๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•ด๋ณด์ž!

๋ฐ˜์‘ํ˜•