-
Notifications
You must be signed in to change notification settings - Fork 1
/
3273.py
51 lines (34 loc) · 914 Bytes
/
3273.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 시간 초과 코드
# boj, 3273 : 두 수의 합, python3
# 투 포인터, 정렬
import sys
def solution(n, l, x):
result = 0
for i in range(n):
for j in range(i + 1, n):
if l[i] + l[j] == x:
result += 1
return result
n = int(sys.stdin.readline())
sequence = list(map(int, sys.stdin.readline().split()))
x = int(sys.stdin.readline())
print(solution(n, sequence, x))
# 성공 코드
import sys
def solution(n, l, x):
left, right = 0, n - 1
result = 0
while left < right:
target = l[left] + l[right]
if target == x:
result += 1
left += 1
elif target < x:
left += 1
else:
right -= 1
return result
n = int(sys.stdin.readline())
sequence = sorted(list(map(int, sys.stdin.readline().split())))
x = int(sys.stdin.readline())
print(solution(n, sequence, x))