코드
n = int(input())
armor = int(input())
iron = sorted(list(map(int, input().split())))
start, end = 0, n-1
count = 0
while start < end:
if iron[start] + iron[end] > armor: end -= 1
elif iron[start] + iron[end] < armor: start += 1
else:
count += 1
start += 1
end -= 1
print(count)
투 포인터 알고리즘을 이용해 순차적으로 리스트에 접근하는 방식으로 풀면 된다.
'백준' 카테고리의 다른 글
[백준] 4158번 CD 파이썬 코드 (1) | 2023.09.07 |
---|---|
[백준] 2003번 수들의 합2 파이썬 코드 (0) | 2023.09.07 |
[백준] 2776번 암기왕 파이썬 코드 (0) | 2023.09.06 |
[백준] 1654번 랜선 자르기 파이썬 코드 (0) | 2023.09.06 |
[백준] 2512번 예산 파이썬 코드 (0) | 2023.09.05 |
코드
n = int(input())
armor = int(input())
iron = sorted(list(map(int, input().split())))
start, end = 0, n-1
count = 0
while start < end:
if iron[start] + iron[end] > armor: end -= 1
elif iron[start] + iron[end] < armor: start += 1
else:
count += 1
start += 1
end -= 1
print(count)
투 포인터 알고리즘을 이용해 순차적으로 리스트에 접근하는 방식으로 풀면 된다.
'백준' 카테고리의 다른 글
[백준] 4158번 CD 파이썬 코드 (1) | 2023.09.07 |
---|---|
[백준] 2003번 수들의 합2 파이썬 코드 (0) | 2023.09.07 |
[백준] 2776번 암기왕 파이썬 코드 (0) | 2023.09.06 |
[백준] 1654번 랜선 자르기 파이썬 코드 (0) | 2023.09.06 |
[백준] 2512번 예산 파이썬 코드 (0) | 2023.09.05 |