알고리즘 단련장/해커랭크

[해커랭크] Equalize the Array

snapcoder 2025. 12. 10. 20:43
728x90
반응형
SMALL

https://www.hackerrank.com/challenges/equality-in-a-array/problem?isFullScreen=true

 

Equalize the Array | HackerRank

Delete a minimal number of elements from an array so that all elements of the modified array are equal to one another.

www.hackerrank.com

 

Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.정수 배열이 주어졌을 때, 동일한 값의 요소만 남도록 삭제할 최소 원소 수를 결정합니다.

Example본보기

 

Delete the  elements  and  leaving . If both twos plus either the  or the  are deleted, it takes  deletions to leave either  or . The minimum number of deletions is .원소를 삭제하고 . 만약 두 개의 2와 또는 중 하나가 삭제되면, 남기려면 삭제가 필요합니다. 최소 삭제 횟수는 입니다.

Function Description기능 설명

Complete the equalizeArray function in the editor below.아래 편집기에서 equalizeArray 함수를 완성하세요.

equalizeArray has the following parameter(s):equalizeArray는 다음과 같은 매개변수를 가집니다:

  • int arr[n]: an array of integersint arr[n]: 정수 배열입니다

Returns반환

  • int: the minimum number of deletions requiredint: 필요한 최소 삭제 횟수

Input Format입력 형식

The first line contains an integer , the number of elements in .
The next line contains  space-separated integers .첫 번째 줄은 정수 를 포함하며, 에 속하는 원소 수를 의미합니다. 다음 줄은 공간 분리 정수 를 포함한다.

Constraints제약 조건

  •  
  •  

Sample Input샘플 입력

STDIN       Function
-----       --------
5           arr[] size n = 5
3 3 2 1 3   arr = [3, 3, 2, 1, 3]

Sample Output샘플 출력

2   

Explanation설명

Delete  and  to leave . This is minimal. The only other options are to delete  elements to get an array of either  or .삭제하고 떠나세요. 이 정도는 최소한입니다. 다른 방법은 요소를 삭제하여 또는 의 배열을 만드는 것뿐입니다.

 

 

풀이

101개 배열 만들고, 가장 많이 중복되는 숫자의 중복 개수를 찾고,
전체 사이즈에서 빼주어 해결했습니다.

 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'equalizeArray' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static int equalizeArray(List<Integer> arr) {
    // Write your code here
        int dap = 1;
        Integer[] count = new Integer[101];
        Arrays.fill(count, 0);
       
        for(int num : arr){
            count[num]++;
        }
       
        List<Integer> orderby = Arrays.asList(count);
       
        int maxValue = Collections.max(orderby);
        dap = arr.size()-maxValue;
       
        return dap;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.equalizeArray(arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

 

728x90
반응형
LIST