백준 11728번: 배열 합치기

danbibibi·2021년 11월 20일
0

문제

문제 바로가기> 백준 11728번: 배열 합치기

풀이

두 배열을 입력 받은 후, idx를 옮겨가면서 작은 순으로 출력해주었다.

def solution():
    import sys
    input = sys.stdin.readline
    size1, size2 = map(int, input().split())
    li1 = list(map(int, input().split()))
    li2 = list(map(int, input().split()))
    idx1, idx2 = 0, 0
    while True:
        if idx1<size1 and idx2<size2:
            if li1[idx1] < li2[idx2]:
                print(li1[idx1], end=' ')
                idx1+=1
            else:
                print(li2[idx2], end=' ')
                idx2+=1
        else:
            while(idx1<size1):
                print(li1[idx1], end=' ')
                idx1 += 1
            while(idx2<size2):
                print(li2[idx2], end=' ')
                idx2 += 1
            break
solution()

다른 방법

다음과 같이 풀면 메모리는 더 많이 사용되지만 시간은 훨씬 절약된다.

def solution():
    import sys
    input()
    print(' '.join(sorted(sys.stdin.read().split(), key=int)))
solution()
profile
블로그 이전) https://danbibibi.tistory.com

0개의 댓글

관련 채용 정보