[ํ๋ก๊ทธ๋๋จธ์ค | Lv2] ์๊ถ๋ํ (Python) - 2022 KAKAO BLIND RECRUITMENT
https://school.programmers.co.kr/learn/courses/30/lessons/92342
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
BFS๋ฅผ ์ด์ฉํ์ฌ ํ์๋ค.
ํ์ด์ ์ ์ ์๋ ๊ฒฝ์ฐ (๋จ์ ํ์ด์ด ์กด์ฌ)
10์ ๋ถํฐ ์ฐจ๋ก๋๋ก ํ์ด์ ์๋ค.
์ ์ ์๋ ํ์ด์ ์ดํผ์น๋ณด๋ค 1๋ฐ ๋ ์๊ธฐ or 0๋ฐ ์๊ธฐ์ด๋ค.
deque์ ์ ์ ์๋ ํ์ด ๋ฆฌ์คํธ๋ฅผ ๋ชจ๋ ๊ฐฑ์ ํ์ฌ ์ ์ฅํ๋ค.
๋์ด์ ์ ํ์ด์ด ์๋ ๊ฒฝ์ฐ
10~1์ ๊น์ง ๋ชจ๋ ํ์ด์ ๋ค ์๊ณ , ๋จ์ ํ์ด์ด ์กด์ฌํ ๊ฒฝ์ฐ 0์ ์ ๋จ์ ํ์ด์ ๋ชฐ์์ค๋ค.
๋ผ์ด์ธ๊ณผ ์ดํผ์น์ ์ ์๋ฅผ ๊ณ์ฐํ์ฌ ๋์ ์ ์์ฐจ๊ฐ ๊ฐ์ฅ ํฐ ๊ฐ์ผ๋ก ์ ๋ต์ ๊ฐฑ์ ํ๋ค.
์ต๋๊ฐ์ด ์ฌ๋ฌ๊ฐ์ผ ๊ฒฝ์ฐ, answer์ ํด๋น ๋ฆฌ์คํธ๋ฅผ ์ถ๊ฐํด์ค๋ค.
answer์ด ๋น ๊ฐ์ด ์๋ ๊ฒฝ์ฐ
์์๋ฅผ ๊ฑฐ๊พธ๋ก ์ ๋ ฌํ์ฌ ๊ฐ์ฅ ๋ง์ง๋ง ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํ๋ค. (๊ฐ์ฅ ๋ฎ์ ์ ์๋ฅผ ๋ ๋งํ ๋ฆฌ์คํธ)
answer์ด ๋น ๊ฐ์ธ ๊ฒฝ์ฐ
[-1]์ ๋ฆฌํดํด์ค๋ค. (๋ผ์ด์ธ์ด ์ดํผ์น๋ฅผ ์ด๊ธธ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ์กด์ฌํ์ง ์๋๋ค.)
# ํ๋ก๊ทธ๋๋จธ์ค 92342: ์๊ถ๋ํ (2022 KAKAO BLIND RECRUITMENT)
from collections import deque
def solution(n, info):
answer = []
res = 1 # ๋น๊ธฐ๋ ๊ฒฝ์ฐ ๊ณ ๋ คํ์ฌ 1๋ก ์ด๊ธฐํ
queue = deque()
queue.append([0, [0] * 11]) # [pos, score]
while queue:
pos, q = queue.pop()
if sum(q) == n or (sum(q) < n and pos == 9): # 10~1์ ๊น์ง ๋ชจ๋ ํ์ด์ ๋ค ์ ๊ฒฝ์ฐ
if (sum(q)) < n:
q[10] = n - sum(q) # 0์ ์ ๋จ์ ํ์ด ๋ชฐ์์ฃผ๊ธฐ
ryan = 0
apeach = 0
for i in range(10): # ์ ์ ๊ณ์ฐ (0์ ์ ๊ณ์ฐํ ํ์ X)
if q[i] > 0:
ryan += 10 - i
elif info[i] > 0:
apeach += 10 - i
if res < ryan-apeach: # ์ต๋๊ฐ ๊ฐฑ์
answer = [q]
res = ryan - apeach
elif res == ryan-apeach:
answer.append(q)
elif sum(q) < n: # ๋จ์ ํ์ด์ด ์กด์ฌํ ๊ฒฝ์ฐ
q1 = q.copy()
q2 = q.copy()
q1[pos] = 0
q2[pos] = info[pos] + 1
pos += 1
queue.append([pos, q1])
queue.append([pos, q2])
if answer:
answer.sort(key = lambda x : x[::-1]) # ์์๋ฅผ ๊ฑฐ๊พธ๋ก ์ ๋ ฌ
return answer[-1]
return [-1]
ํ๊ธฐ ๋๋ฌด ํ๋ ๋ฌธ์ ์๋ค,,,, ๋ถ๋ฐํ์ ๐๐ฅฒ