From 443e2b27fc77b59001c09ae8b483d4f1122c53cc Mon Sep 17 00:00:00 2001 From: Gyunseo Lee Date: Thu, 18 Apr 2024 20:30:22 +0900 Subject: [PATCH] 2024-04-18 20:30:22 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: src/content/blog/boj-2230-수-고르기.md --- ...0-\352\263\240\353\245\264\352\270\260.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/src/content/blog/boj-2230-\354\210\230-\352\263\240\353\245\264\352\270\260.md" "b/src/content/blog/boj-2230-\354\210\230-\352\263\240\353\245\264\352\270\260.md" index 5c07faf73..826f85b02 100644 --- "a/src/content/blog/boj-2230-\354\210\230-\352\263\240\353\245\264\352\270\260.md" +++ "b/src/content/blog/boj-2230-\354\210\230-\352\263\240\353\245\264\352\270\260.md" @@ -38,3 +38,37 @@ if __name__ == "__main__": print(minAns) ``` + +## Two Pointers를 이용한 방법 + +```python +import sys +from bisect import bisect_left + +input = sys.stdin.readline +MAX = 2 * int(1e9) + 1 +minAns = MAX + + +def OOB(j): + if j < 0 or j >= N: + return True + return False + + +if __name__ == "__main__": + N, M = map(int, input().rstrip().split()) + A = sorted([int(input().rstrip()) for _ in range(N)]) + e = 0 + for s in range(len(A)): + while not OOB(e) and A[e] - A[s] < M: + e += 1 + if e == len(A): + break + minAns = min(minAns, A[e] - A[s]) + + print(minAns) +``` + +인덱스 하나 차이로 런타임 오류나 WA를 판정 받을 수 있다. +꼼꼼히 OOB 함수등을 이용해서, 인덱스 체크를 하자.