샘성의 iOS 개발 일지

[Warm Up] Birthday Cake Candles 본문

Algorithm/HackerRank

[Warm Up] Birthday Cake Candles

SamusesApple 2023. 5. 11. 14:27
728x90

문제 설명:

  You are in charge of the cake for a child's birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.

 

 

 

내 풀이:

// should return the number of tallest candle they have
func birthdayCakeCandles(candles: [Int]) -> Int {
	// 큰 순서대로 배열 sorted 후, 첫번째 인덱스의 값 추출 (가장 큰 값)
    var height = candles.sorted(by: >)[0]
    // 가장 큰 값인 candle만 filter하여 배열에 담은 후, 배열의 요소 갯수 count
    return candles.filter { $0 == height }.count
}
728x90

'Algorithm > HackerRank' 카테고리의 다른 글

[Implementation] Apple and Orange  (0) 2023.05.12
[Implementation] Grading Students  (0) 2023.05.12
[Warm Up] Time Conversion  (0) 2023.05.11
[Warm Up] Staircase  (0) 2023.05.11
[Warm Up] Diagonal Difference  (0) 2023.05.11