백준 3273 두 수의 합

김민영·2023년 2월 15일
0

알고리즘

목록 보기
123/125

과정

  • 투포인터
  • 시작점을 0, 끝점을 마지막으로 해서
  • 두 수를 더한 값이 X와 같으면 세고,
  • X보다 작으면 시작점 +1
  • X보다 크면 끝점 -1을 한다.
N = int(input())
lst = list(map(int, input().split()))
X = int(input())
lst.sort()

s = 0
e = N-1
cnt = 0
while s < e:
    hap = lst[s] + lst[e]
    if hap == X:
        cnt += 1
        s += 1
        e -= 1
    elif hap < X:
        s += 1
    elif hap > X:
        e -= 1

print(cnt)```
profile
노션에 1차 정리합니당 - https://cream-efraasia-f3c.notion.site/4fb02c0dc82e48358e67c61b7ce8ab36?v=

0개의 댓글