알고리즘
두 배열의 원소 교체
메밀국수가생각나
2021. 12. 19. 12:12
나의 풀이
n, k = map(int, input().split())
sum = 0
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort(reverse = True)
for i in range(k):
a[i] = b[i]
for i in range(n):
sum += a[i]
print(sum)
정답
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort(reverse=True)
for i in range(k):
if a[i] < b[i]:
a[i], b[i] = b[i], a[i]
else:
break
print(sum(a))
정답을 본 후 나의 풀이
n, k = map(int, input().split())
sum = 0
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort(reverse = True)
for i in range(k):
if a[i] < b[i]:
a[i] = b[i]
else:
break
for i in range(n):
sum += a[i]
print(sum)
b의 가장 큰값이 a의 가장 작은 값보다 작을 수 있다는 가정을 빼먹고 문제를 풀었다. 정답을 본 후 그것을 빼먹었다는 것을 깨달았다.