본문 바로가기
1 Day 1 Algorithms

[2019.02.12] Migratory Birds

by 곰돌찌 2019. 2. 12.

Problem


You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.

For example, assume your bird sightings are of types . There are two each of types and , and one sighting of type . Pick the lower of the two types seen twice: type .

Function Description

Complete the migratoryBirds function in the editor below. It should return the lowest type number of the most frequently sighted bird.

migratoryBirds has the following parameter(s):

  • arr: an array of integers representing types of birds sighted

Input Format

The first line contains an integer denoting , the number of birds sighted and reported in the array 
The second line describes  as  space-separated integers representing the type numbers of each bird sighted.

Constraints

  • It is guaranteed that each type is , , , , or .

Output Format

Print the type number of the most common bird; if two or more types of birds are equally common, choose the type with the smallest ID number.

Sample Input 0

6
1 4 4 4 5 3

Sample Output 0

4

Explanation 0

The different types of birds occur in the following frequencies:

  • Type  bird
  • Type  birds
  • Type  bird
  • Type  birds
  • Type  bird

The type number that occurs at the highest frequency is type , so we print  as our answer.

Sample Input 1

11
1 2 3 4 5 4 3 2 1 3 4

Sample Output 1

3

Explanation 1

The different types of birds occur in the following frequencies:

  • Type 
  • Type 
  • Type 
  • Type 
  • Type 

Two types have a frequency of , and the lower of those is type .


How I solved the problem


# 제한에서 새의 타입은 총 5개 이므로 count용 array의 크기를 5로 지정! 

# array의 index + 1이 새의 타입으로 생각하면 됨!


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;

public class Solution {

// Complete the migratoryBirds function below.
static int migratoryBirds(List<Integer> arr) {
//constraints : type 1 ~ 5
int[] birdTypeArr = new int[5];

for (int i = 0; i < arr.size(); i++) {
int birdType = arr.get(i);
birdTypeArr[birdType - 1]++; //plus 1 in birdType - 1
}
int frequencyType = 0;
int frequency = 0;

for (int i = 0; i < birdTypeArr.length; i++) {
if (birdTypeArr[i] > frequency) { //if bird frequency is bigger
frequency = birdTypeArr[i];
frequencyType = i + 1; //birdType is arr's index + 1
}
}

return frequencyType;
}

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 arrCount = Integer.parseInt(bufferedReader.readLine().trim());

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

int result = migratoryBirds(arr);

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

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


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.02.14] Bon Appetit  (0) 2019.02.14
[2019.02.13] Day of the Programmer  (0) 2019.02.13
[2019.02.11] Divisible Sum Pairs  (0) 2019.02.11
[2019.02.08] Birthday Chocolate  (0) 2019.02.08
[2019.02.01] Breaking the Records  (0) 2019.02.01

댓글