구름 LEVEL : 개미 집합의 지름

후웅후웅·2024년 8월 1일

알고리즘

목록 보기
5/10

문제

import java.util.*;
class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int N = sc.nextInt();
		int D = sc.nextInt();
		int ant[] = new int[N];
		for(int i = 0; i < N; i++){
			ant[i] = sc.nextInt();
		}
		
		Arrays.sort(ant);

		int start = 0;
		int end = 0;
		int liveAnt = 0;
		while(start < N && end < N){
			if(ant[end] - ant[start] <= D){
				liveAnt = Math.max(liveAnt, end - start + 1);
				end++;
			} else {
				start++;
			}
		}
		
		System.out.println(N - liveAnt);
	}
}
profile
뭐든 열심히

0개의 댓글