Q. 이미 오름차순으로 정렬되어있는 두 리스트를 합쳐서 오름차순으로 정렬하시오
A.
sort 함수는 시간복잡도가 nlogn
이미 정렬되어있기 때문에 더 빠른 포인터 변수 를 사용하자
an = int(input())
a = list(map(int, input().split()))
bn = int(input())
b = list(map(int, input().split()))
new = []
p1, p2 = 0, 0
while True:
if a[p1] < b[p2]:
new.append(a[p1])
p1 += 1
if p1 == an:
new += b[p2:]
break
elif a[p1] >= b[p2]:
new.append(b[p2])
p2 += 1
if p2 == an:
new += a[p1:]
break
print(*new)
출처 : 파이썬 알고리즘 문제풀이 입문 강의