728x90
https://www.acmicpc.net/problem/2230
2230번: 수 고르기
N개의 정수로 이루어진 수열 A[1], A[2], …, A[N]이 있다. 이 수열에서 두 수를 골랐을 때(같은 수일 수도 있다), 그 차이가 M 이상이면서 제일 작은 경우를 구하는 프로그램을 작성하시오. 예를 들어
www.acmicpc.net
💡 문제 풀이
투포인터 문제이다.
요구사항에 맞게 구현하면 된다.
✔️ 느낀 점
이분 탐색을 풀고 싶었는데 투포인터에 더 가까운 문제라고 생각된다.
어려울 것이 없어서 패스...
💻 코드
import sys
input = sys.stdin.readline
def solution(n, m, arr):
start, end = 0, 1
min_ = sys.maxsize
while start < n and end < n:
calc = arr[end] - arr[start]
if calc == m: return m
if calc < m:
end += 1
continue
start += 1
min_ = min(min_, calc)
return min_
n, m = map(int, input().split())
arr = []
for _ in range(n):
arr.append(int(input().rstrip()))
arr.sort()
print(solution(n, m, arr))
'문제 풀이 > 백준(BOJ)' 카테고리의 다른 글
[Python] 백준 17396번 : 백도어 (0) | 2023.02.25 |
---|---|
[Python] 백준 1956번 : 운동 (0) | 2023.02.23 |
[Python] 백준 13023번 : ABCDE (0) | 2023.02.23 |
[Python] 백준 17609번 : 회문 (0) | 2023.02.22 |
[Python] 백준 14502번 : 연구소 (0) | 2023.02.22 |
댓글