BOJ_2230_수고르기

Bro_Jang·2025년 2월 5일

Algorithm

목록 보기
9/15
post-thumbnail

걸린 시간: 30m

런타임 에러 (ArrayIndexOutOfBounds) 고치느라 시간을 과도하게 사용

알고리즘 분류: Two Pointer

package BOJ_2230_수고르기;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int mindiff = sc.nextInt();
        int[] nums = new int[N];

        for(int i = 0 ; i < N; i++){
            nums[i] = sc.nextInt();
        }

        Arrays.sort(nums);

        int left = 0;
        int right = 0;

        int ans = Integer.MAX_VALUE;

        while(left < N && right < N) {
            int diff = nums[right] - nums[left];

            if(diff >= mindiff){
                ans = Math.min(diff, ans);
                left++;
            }
            else{
                right++;
            }
        }
        System.out.println(ans);
    }
}
profile
개발 해봐야지

0개의 댓글