본 내용은 파이썬 알고리즘 문제풀이 (코딩테스트 대비) 강의를 토대로 작성하였습니다.

# 두 리스트 합치기
import sys
#sys.stdin=open("input.txt", "r")
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = list(map(int, input().split()))
for x in a:
b.append(x)
b.sort()
for x in b:
print(x, end = " ")
단순하게 풀이한 방법이다.
아래와 같은 코드를 작성하도록 하자.
import sys
#sys.stdin=open("input.txt", "r")
n=int(input())
a=list(map(int, input().split()))
m=int(input())
b=list(map(int, input().split()))
p1=p2=0
res = []
# 각 리스트의 끝 : n, m
# 두 리스트 중 작은 리스트 먼저 처리
while p1<n and p2<m:
if a[p1] < b[p2]:
res.append(a[p1])
p1 += 1
else :
res.append(b[p2])
p2 += 1
# 처리하고 남은게 있다면
if p1<n:
res = res + a[p1::]
if p2<m:
res = res + b[p2::]
for x in res :
print(x, end=' ')
'Algorithm > 파이썬 알고리즘 문제풀이' 카테고리의 다른 글
[python/inflearn/section3] 격자판 최대합 (1) | 2022.09.25 |
---|---|
[python/inflearn/section3] 수들의 합 (0) | 2022.09.22 |
[python/inflearn/section3] 카드 역배치(정올 기출) (0) | 2022.09.22 |
[python/inflearn/section3] 숫자만 추출 (0) | 2022.09.22 |
[python/inflearn/section3] 회문 문자열 검사 (0) | 2022.09.22 |