๋ฐ์ํ
๐ฑ ๋ฌธ์
๐ฑ ํ์ด
ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ํธ๋ ๋ฌธ์ ์
๋๋ค. ํต์ฌ ์์ด๋์ด๋ ๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ด m
์ด์์ผ ๋, result
๋ฅผ ์
๋ฐ์ดํธํด ๋๊ฐ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฆฌ๊ณ ์
๋ฐ์ดํธ์ ๋์์ ์ผ์ชฝ ํฌ์ธํฐ๋ฅผ ํ๋ ์ฆ๊ฐ์ํต๋๋ค. ์ฐจ์ด๊ฐ ๋ ์ค์ฌ์ง ์ ์๋์ง ํ์ธํ๊ธฐ ์ํด์์
๋๋ค. ๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ด m
๋ฏธ๋ง์ด๋ผ๋ฉด ์ค๋ฅธ์ชฝ ํฌ์ธํฐ๋ฅผ ํ๋ ์ฆ๊ฐ์์ผ, ์ฐจ์ด๋ฅผ ํค์์ค๋๋ค.
๐ฑ ์ฝ๋
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# boj 2230 ์ ๊ณ ๋ฅด๊ธฐ
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
array = []
for _ in range(n):
array.append(int(input().rstrip()))
array.sort()
result = sys.maxsize
start, end = 0, 0
while start < n and end < n:
diff = array[end] - array[start]
if diff < m:
end += 1
else:
result = min(result, diff)
start+= 1
print(result)
๋ฐ์ํ
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #13144 List of Unique Numbers (Python) (0) | 2023.02.21 |
---|---|
[BOJ] #18405 ๊ฒฝ์์ ์ ์ผ (Python) (0) | 2023.02.15 |
[BOJ] #9935 ๋ฌธ์์ด ํญ๋ฐ (Python) (0) | 2023.02.13 |
[BOJ] #2473 ์ธ ์ฉ์ก (Python) (0) | 2023.02.13 |
[BOJ] #2470 ๋ ์ฉ์ก (Python) (0) | 2023.02.13 |
๋๊ธ