Skip to content

Commit 0b22431

Browse files
author
hangyeol
committed
126차 2번 문제풀이
1 parent c7cf223 commit 0b22431

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
def solution(n, info):
2+
maxDiff = -1
3+
answer = [-1]
4+
5+
# DFS로 점수 구간을 하나씩 탐색하면서 라이언의 경우를 모두 탐색
6+
def dfs(index, arrowsLeft, ryanShots):
7+
nonlocal maxDiff, answer
8+
9+
# 모든 구간을 다 고려했을 때
10+
if index == 11:
11+
if arrowsLeft > 0:
12+
ryanShots[10] += arrowsLeft
13+
14+
ryanScore = 0
15+
apeachScore = 0
16+
17+
# 각 점수 계산
18+
for i in range(11):
19+
if info[i] == 0 and ryanShots[i] == 0:
20+
continue
21+
if ryanShots[i] > info[i]:
22+
ryanScore += 10 - i
23+
else:
24+
apeachScore += 10 - i
25+
26+
# 라이언이 이긴 경우
27+
if ryanScore > apeachScore:
28+
diff = ryanScore - apeachScore
29+
if diff > maxDiff:
30+
maxDiff = diff
31+
answer = ryanShots[:]
32+
elif diff == maxDiff:
33+
for i in range(10, -1, -1):
34+
if ryanShots[i] > answer[i]:
35+
answer = ryanShots[:]
36+
break
37+
elif ryanShots[i] < answer[i]:
38+
break
39+
40+
if arrowsLeft > 0:
41+
ryanShots[10] -= arrowsLeft
42+
return
43+
44+
# 현재 점수를 얻기 위해 필요한 화살 수
45+
neededArrows = info[index] + 1
46+
if neededArrows <= arrowsLeft:
47+
# 이 점수를 가져가는 경우
48+
ryanShots[index] = neededArrows
49+
dfs(index + 1, arrowsLeft - neededArrows, ryanShots)
50+
ryanShots[index] = 0
51+
52+
# 이 점수를 포기하는 경우
53+
dfs(index + 1, arrowsLeft, ryanShots)
54+
55+
dfs(0, n, [0] * 11)
56+
return answer

0 commit comments

Comments
 (0)