문제링크
https://www.acmicpc.net/problem/14889
코드 제출 기록 (메모리 및 시간)
제출 날짜
@4/22/2021
메모리
149476 KB
시간
1252 ms
메모
•
삼성 sw 코딩테스트 기출문제
문제풀이
파이썬으로 조합 만들기
def my_combinations(arr, r):
for i in range(len(arr)):
if r == 1:
yield [arr[i]]
else:
for next in my_combinations(arr[i+1:], r-1):
yield [arr[i]] + next
Python
복사
Code
N = int(input())
power = [list(map(int, input().split())) for _ in range(N)]
member = []
for i in range(N):
member.append(i)
def my_combinations(arr, r):
for i in range(len(arr)):
if r == 1:
yield [arr[i]]
else:
for next in my_combinations(arr[i+1:], r-1):
yield [arr[i]] + next
team = []
for comb in my_combinations(member, N//2):
tmp = 0
for comb2 in my_combinations(comb, 2):
tmp += power[comb2[0]][comb2[1]] + power[comb2[1]][comb2[0]]
team.append(tmp)
answer = abs(team[0] - team[-1])
for i in range(len(team)//2):
answer = min(answer, abs(team[i] - team[-i-1]))
print(answer)
Python
복사