
1
2
3
4
5
6
7
8
9
|
import sys
n=int(input())
m=int(input())
ingredientList=list(map(int, sys.stdin.readline().split()))
ans=0
for i in range(n):
for j in range(i+1,n):
if ingredientList[i]+ingredientList[j]==m: ans+=1
print(ans)
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
n=int(input())
m=int(input())
ingredientList=list(map(int, input().split()))
ingredientList.sort()
start,end=0,n-1
ans=0
while start < end:
target=ingredientList[start]+ingredientList[end]
if target==m:
ans+=1
end-=1
start+=1
elif target>m: end-=1
else : start+=1
print(ans)
|
cs |
처음에는 pypy3 - 시간복잡도 (n**2) 로 구현했는데
밑에 분류 보니까 투포인터 라고 써있어서
아래처럼 투포인터로 구현했습니다
sort하기 어려운 input이라면 또 어떻게 구현할지.. 궁금하네요
반응형
'python-algorithm' 카테고리의 다른 글
백준 2485 가로수 (0) | 2021.12.27 |
---|---|
백준 5347 LCM (0) | 2021.12.27 |
백준 14490 백대열 (0) | 2021.12.27 |
백준 2670 연속부분최대곱 (0) | 2021.12.23 |
백준 2776 암기왕 (0) | 2021.12.23 |
댓글