AI 공부 도전기

[Baekjoon] 2470번 : 두 용액 (Python, 정렬)

 

     

 

 

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

 

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

시간 초과 - 방법 1 -  코드 길이 245B

 

n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = []
d = 1e10
for i in range(n-1):
    for j in range(i+1, n):
        diff = abs(l[i] + l[j])
        if diff < d:
            d = diff
            ans = (l[i], l[j])
print(*ans)

 

방법 2 - 메모리 41532KB / 시간 160ms / 코드 길이 317B

 

n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = []
left, right = 0, n-1
d = 1e10
while left < right:
    lv = l[left]
    rv = l[right]
    diff = lv + rv
    if abs(diff) < d:
        d = abs(diff)
        ans = (lv, rv)
    if diff < 0:
        left += 1
    else:
        right -= 1
print(*ans)

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading