๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ์นดํŽซ Programmers(Lv.2)

by seolhee2750 2022. 2. 3.
๋ฌธ์ œ

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

 

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

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

programmers.co.kr

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
def solution(brown, yellow):
    answer = []

    for i in range(3, brown):
        if (i - 2) * ((brown - (i * 2)) / 2) == yellow:
            answer = [(brown - (i * 2)) / 2 + 2, i]
            break

    return answer

๐Ÿ‘‰ ์™„์ „ํƒ์ƒ‰ ๋ฌธ์ œ๋กœ, ์นดํŽซ์ด ๊น”๋ฆฌ๋Š” ๊ทœ์น™์„ ์ƒ๊ฐํ•ด์„œ ํ’€์–ด์ฃผ๋ฉด ๋œ๋‹ค.

๊ฐ€์žฅ ์ž๋ฆฌ์˜ ๋”ฑ ํ•œ์นธ์”ฉ๋งŒ ๊ฐˆ์ƒ‰์ด ๊น”๋ฆฌ๊ณ , ๋‚˜๋จธ์ง€๋Š” ๋…ธ๋ž€์ƒ‰์ด๋ฏ€๋กœ ์ „์ฒด ์นดํŽซ์˜ ์ตœ์†Œ ํ•œ ๋ณ€ ๊ธธ์ด๋Š” 3์ด๋‹ค.

๋”ฐ๋ผ์„œ 3๋ถ€ํ„ฐ for๋ฌธ์„ ๋Œ๋ ค์ฃผ์—ˆ๊ณ , ์„ธ๋กœ ๊ธธ์ด๋ฅผ 3๋ถ€ํ„ฐ ๋Š˜๋ ค์ฃผ๋ฉด์„œ, ๋ฐ˜๋ณต๋งˆ๋‹ค ๋…ธ๋ž€์ƒ‰์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์นธ ์ˆ˜๋ฅผ ๊ตฌํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๊ฐˆ์ƒ‰๊ณผ ๋…ธ๋ž€์ƒ‰์˜ ์ƒ๊ด€ ๊ด€๊ณ„๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด์ฃผ๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€