푸는데 진짜 오래걸렸다....
중복조합을 이용한 완전탐색으로 점수차의 최대값을 갱신하면서 풀었다
몇개 숙지해야할점이 보여서 남기는 글
소스코드
from itertools import combinations_with_replacement
def solution(n, info):
answer = [0]*len(info)
maximum=0
info=info[::-1]
if n>1:
for indexes in combinations_with_replacement(range(11),n):
for i in indexes:
answer[i]+=1
if compare(info,answer)>maximum:
maximum=compare(info,answer)
can=answer[:]
answer = [0]*len(info)
else:
candidate=[0,1,2,3,4,5,6,7,8,9,10]
for i in candidate:
answer[i]=1
if compare(info,answer)>maximum:
maximum=compare(info,answer)
can=answer[:]
answer[i]=0
if maximum==0:return[-1]
else:
return can[::-1]
def compare(info,answer):
answersum=0
infosum=0
for i in range(len(info)):
if answer[i]==0 and info[i]==0:continue
if answer[i]>info[i]:
answersum+=i
else:
infosum+=i
if answersum>infosum:
return answersum-infosum
else:
return 0
info[::-1] :뒤집기
combinations_with_replacement 와 product의 차이
deepcopy[:]
to be updated