Birthday Cake Candles

  • + 0 comments

    Python solution using simple for loop (a bit verbose but readable).

    Algorithms

    1. Reverse sort the candles (highest numbers are placed in the starting positions)
    2. Loop candles starting from first highest number
    3. Compare if first highest number is equal or greater than previous_tallest
    4. If #3 evaluates to True, assign the first highest number to previous_tallest and increment tallest_counts by 1
    5. Repeat step #2 to #4
    6. 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