Problem
You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.
For example, if your niece is turning years old, and the cake will have candles of height , , , , she will be able to blow out candles successfully, since the tallest candles are of height and there are such candles.
Function Description
Complete the function birthdayCakeCandles
in the editor below. It must return an integer representing the number of candles she can blow out.
birthdayCakeCandles has the following parameter(s):
- ar: an array of integers representing candle heights
Input Format
The first line contains a single integer, , denoting the number of candles on the cake.
The second line contains space-separated integers, where each integer describes the height of candle .
Constraints
Output Format
Print the number of candles that can be blown out on a new line.
Sample Input 0
4
3 2 1 3
Sample Output 0
2
Explanation 0
We have one candle of height , one candle of height , and two candles of height . Your niece only blows out the tallest candles, meaning the candles where . Because there are such candles, we print on a new line.
How I solved the problem
# 가장 키가 큰 초의 개수를 세는 것이기 때문에 sorting을 한 후에 크기가 같은 초들의 개수를 센 후에 break하면 됨.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the birthdayCakeCandles function below. static int birthdayCakeCandles(int[] ar) { int result = 0; Arrays.sort(ar); //sort -- 오름차순 정렬 int bigOne = ar[ar.length - 1]; //키가 가장 큰 수 //내림차순으로 정렬하려면 array를 list로 만든 후, Collections.reverse로 하면됨 for (int i = ar.length - 1; i >= 0; i--) { //위에서 부터 아래로! if (bigOne == ar[i]) { result++; } if (bigOne > ar[i]) { break; //정렬되어 있어서 큰 수보다 작아지는 순간 부터 비교할 필요 없음. } } return result; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int arCount = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); int[] ar = new int[arCount]; String[] arItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int i = 0; i < arCount; i++) { int arItem = Integer.parseInt(arItems[i]); ar[i] = arItem; } int result = birthdayCakeCandles(ar); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.01.28] Grading Students (0) | 2019.01.28 |
---|---|
[2019.01.25] Time Conversion (0) | 2019.01.25 |
[2019.01.23] Bitwise AND (1) | 2019.01.23 |
[2019.01.22] RegEx, Patterns, and Intro to Databases (0) | 2019.01.22 |
[2019.01.21] Testing (0) | 2019.01.21 |
댓글