๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

โฌ› [Programmers] [Python] [Level3] ์ตœ๊ณ ์˜ ์ง‘ํ•ฉ

Dbswnstjd 2023. 4. 6. 14:45

๋ฌธ์ œ

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

 

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

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

programmers.co.kr

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 3๋‹จ๊ณ„ - ์ตœ๊ณ ์˜ ์ง‘ํ•ฉ
def solution(n, s):
    answer = []
    if s < n: return [-1]
    
    for i in range(n):
        answer.append(s//n)
    idx = len(answer) - 1
    for j in range(s % n):
        answer[idx] += 1
        idx -= 1
    return answer
print(solution(2,9))

์ง‘ํ•ฉ์˜ ๊ณฑ์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋ ค๋ฉด ๊ฐ๊ฐ์˜ ์ˆซ์ž์˜ ์ฐจ์ด๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค. 

๋”ฐ๋ผ์„œ s ๋ฅผ n์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค. ๊ทธ ํ›„ ๋‚˜๋จธ์ง€๋ฅผ ๋’ค์˜ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ 1์”ฉ ์ถ”๊ฐ€ํ•ด ์ฃผ๋„๋ก ํ•œ๋‹ค.