[백준] 2470번 두 용액

거북이·2023년 2월 27일
0

백준[골드5]

목록 보기
33/82
post-thumbnail

💡문제접근

  • 이전 포스팅에 있었던 [[백준] 2467번 용액]과의 차이라면 정렬의 유무이다.
    오름차순 정렬만 해준다면 쉽게 해결할 수 있다.

💡코드(메모리 : 42172KB, 시간 : 128ms)

import sys
input = sys.stdin.readline

N = int(input())
solution = list(map(int, input().strip().split()))
solution.sort()

p1 = 0
p2 = len(solution) - 1
Min = sys.maxsize
while True:
    if p1 >= p2:
        break
    if abs(solution[p1] + solution[p2]) < Min:
        Min = abs(solution[p1] + solution[p2])
        a = solution[p1]
        b = solution[p2]

    if solution[p1] + solution[p2] > 0:
        p2 -= 1
    elif solution[p1] + solution[p2] < 0:
        p1 += 1
    else:
        break
print(a, b)

💡소요시간 : 10m

0개의 댓글