본문 바로가기

CodingTest/Baekjun Online Judge

[ BOJ / 파이썬 ] 3273 두 수의 합

/  제출  1  /

 

처음에 인풋 값 저렇게 큰 줄 모르고 n^2으로 해서 시간 초과

import sys
input = sys.stdin.readline
n = int(input())

nums = list(map(int, input().split()))
x = int(input())
sub_nums = list(map(lambda i: abs(i-x), nums))

count = 0
for sub_num in sub_nums:
  if sub_num in nums:
    count += 1
print(count//2)

 

 

/  제출 2  /

 

https://baby-ohgu.tistory.com/12

 

[백준 3273] 두 수의 합 (Python)

www.acmicpc.net/problem/3273 3273번: 두 수의 합 n개의 서로 다른 양의 정수 a1, a2, ..., an으로 이루어진 수열이 있다. ai의 값은 1보다 크거나 같고, 1000000보다 작거나 같은 자연수이다. 자연수 x가 주어..

baby-ohgu.tistory.com

 

그리고 좋은 풀이

import sys
input = sys.stdin.readline
n = int(input())

nums = list(map(int, input().split()))
x = int(input())

nums.sort()
left, right = 0, n-1

ans = 0
while left < right:
  tmp = nums[left] + nums[right]
  if tmp == x: ans += 1
  if tmp < x:
      left += 1
      continue
  right -= 1
print(ans)

이렇게 푸는 유형이 있는 건 알았지만 이름은 몰랐는데 투포인터라고 부른다고 한다.