백준 / 2470 / 두 용액 / python

맹민재·2023년 5월 17일
0

알고리즘

목록 보기
97/134
n = int(input())
n_list = sorted(list(map(int, input().split())))

left, right = 0, n-1
mix = dict()

while left < right:
    tmp = n_list[left] + n_list[right]
    abs_tmp = abs(tmp)

    if tmp == 0:
        print(n_list[left], n_list[right])
        break

    mix[abs_tmp] = (n_list[left], n_list[right])

    if tmp < 0:
        left += 1
    else:
        right -= 1
else:
    m = min(mix.keys())
    print(*mix[m])

투 포인트 알고리즘을 사용해서 진행했으며 진행 결과를 딕셔너리에 담았다.
절대 값을 key로 했기 때문에 key 중에 최소 값이 0에 가장 가까운 답이 된다.

profile
ㄱH ㅂrㄹ ㅈr

0개의 댓글

관련 채용 정보