11728번 : 배열 합치기 - Python

Pobi·2023년 4월 22일
0

PS

목록 보기
77/107

문제

https://www.acmicpc.net/problem/11728

풀이

배열 A와 배열 B를 가리키는 두개의 포인터(a_index, b_index)를 만든다.두개의 포인터가 가리키는 값을 비교하면서 작은 것들을 넣고, 해당 포인터를 1증가시킨다. 같다면 둘다 넣어주는 식으로 풀이했다.

코드

from sys import stdin

input = stdin.readline

n, m  = map(int,input().split())
a_index = 0
b_index = 0
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list()

while a_index<n and b_index<m:
    if a[a_index]>b[b_index]:
        c.append(b[b_index])
        b_index+=1
    elif a[a_index]<b[b_index]:
        c.append(a[a_index])
        a_index+=1
    elif a[a_index]==b[b_index]:
        c.append(a[a_index])
        c.append(b[b_index])
        a_index+=1
        b_index+=1


for i in range(b_index,m):
    c.append(b[i])
for i in range(a_index,n):
    c.append(a[i])

print(*c)

코드(그냥 파이썬)

input = stdin.readline
nm  = input() #n,m이 필요 없으니 빼버림
a = list(map(int,input().split()))
b = list(map(int,input().split()))

c = a+b

print(*sorted(c))
profile
꿈 많은 개발자

0개의 댓글