[BOJ] #2230 ์ˆ˜ ๊ณ ๋ฅด๊ธฐ (Python)

    ๋ฐ˜์‘ํ˜•

    ๐ŸŒฑ ๋ฌธ์ œ

     

    2230๋ฒˆ: ์ˆ˜ ๊ณ ๋ฅด๊ธฐ

    N๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด A[1], A[2], …, A[N]์ด ์žˆ๋‹ค. ์ด ์ˆ˜์—ด์—์„œ ๋‘ ์ˆ˜๋ฅผ ๊ณจ๋ž์„ ๋•Œ(๊ฐ™์€ ์ˆ˜์ผ ์ˆ˜๋„ ์žˆ๋‹ค), ๊ทธ ์ฐจ์ด๊ฐ€ M ์ด์ƒ์ด๋ฉด์„œ ์ œ์ผ ์ž‘์€ ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด

    www.acmicpc.net

     

    ๐ŸŒฑ ํ’€์ด

    ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ํ‘ธ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š” ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์ด 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)

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€