인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

mw3100162's profile image
mw3100162

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

4. Merge two lists

두리스트 합치기 수업 질문

Written on

·

340

0

n=int(input())
a=list(map(int, input().split()))
m=int(input())
b=list(map(int, input().split()))
res=list(a+b)
res.sort()
print(res)
for i in range(len(res)):
    print(res[i], end=' ')

이런식으로 문제를 풀어도 문제가 없을까요?

코테 준비 같이 해요! python

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 위와 같이 풀어도 상관은 없지만, 제가 이 문제를 만든 이유는 정렬되어 있는 2개의 수열을 2개의 포인터 변수를 써서 정렬된 자료로 병합하는 방법을 설명하기 위해서입니다. 이 과정의 시간복잡도는 O(2n)입니다. sort함수는 시간복잡도가 O(n log n)입니다.

또 영상의 방법이 나중에 재귀함수를 배운 후 병합정렬을 배울때 쓰이니 알아두시면 좋습니다.

mw3100162's profile image
mw3100162

asked

Ask a question