We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 73b3aca commit b372219Copy full SHA for b372219
1 file changed
YoonYn9915/binary search/2025-04-26-[백준]-#2473-세 용액.py
@@ -0,0 +1,43 @@
1
+import sys
2
+
3
+inp = sys.stdin.readline
4
5
+N = int(inp())
6
+arr = list(map(int, inp().split()))
7
+arr.sort()
8
9
+min_value = int(1e10)
10
+answer = [-1, -1, -1]
11
12
+# 각 용액을 순회하며
13
+for i in range(N):
14
+ left = i + 1
15
+ right = N - 1
16
17
+ # i번 용액을 넣었을 때 값이 가장 0에 가까운 두 값 찾기
18
+ while left < right:
19
+ if left == i:
20
21
+ continue
22
+ elif right == i:
23
+ right = i - 1
24
25
26
+ total = arr[i] + arr[left] + arr[right]
27
28
+ # 세 용액의 합이 0에 가장 가까우면 업데이트
29
+ if abs(total) < min_value:
30
+ min_value = abs(total)
31
+ answer = [arr[i], arr[left], arr[right]]
32
33
+ if total == 0:
34
+ break
35
+ elif total < 0:
36
+ left += 1
37
+ else:
38
+ right -= 1
39
40
+answer.sort()
41
42
+for ans in answer:
43
+ print(ans, end=" ")
0 commit comments