We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Python solution using simple for loop (a bit verbose but readable).
Algorithms
Reverse sort the candles (highest numbers are placed in the starting positions)
Loop candles starting from first highest number
Compare if first highest number is equal or greater than previous_tallest
If #3 evaluates to True, assign the first highest number to previous_tallest and increment tallest_counts by 1
Repeat step #2 to #4
Return tallest_counts
def birthdayCakeCandles(candles):
# Sort by reverse order
sorted_candles_desc = sorted(candles, reverse=True)
previous_tallest = 0
tallest_counts = 0
# Loop starting from first highest
for candle in sorted_candles_desc:
# Handle cases where there are more than
# one highest candle (multiple highest equal numbers)
if candle >= previous_tallest:
previous_tallest = candle
tallest_counts += 1
else:
break
return tallest_counts
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Birthday Cake Candles
You are viewing a single comment's thread. Return to all comments →
Python solution using simple for loop (a bit verbose but readable).
Algorithms