less than 1 minute read

Max Min (Hacker Rank)

You will be given a list of integers, , and a single integer . You must create an array of length from elements of such that its unfairness is minimized.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

// The part of the program involving reading from STDIN and writing to STDOUT has been provided by us.

public class Solution {

   public static void main(String[] args) throws NumberFormatException, IOException {

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      int N = Integer.parseInt(in.readLine());
      int K = Integer.parseInt(in.readLine());
      int[] list = new int[N];

      for(int i = 0; i < N; i ++)
         list[i] = Integer.parseInt(in.readLine());

      int unfairness = Integer.MAX_VALUE;
      Arrays.sort(list);
       for(int i = 0; i < N-K+1; i++){
           if(unfairness > list[i+K-1]-list[i]){
               unfairness = list[i+K-1]-list[i];
           }
       }
      /*
       * Write your code here, to process numPackets N, numKids K, and the packets of candies
       * Compute the ideal value for unfairness over here
      */

      System.out.println(unfairness);
   }
}

행운의 77문제 프로젝트
  행운의 77문제 프로젝트는 한 달동안 알고리즘 문제 77개를 푸는 프로젝트입니다.
  (한달은 이미 많이 지났지만...그래도 77문제는 될 수 있도록!)