How can I efficiently find two numbers in an array that sum up to a given target without using the same element twice?

31 views Asked by At

You are given a list of integers representing the height of people standing in a queue. Each person wears a hat with a unique color. However, due to a recent gust of wind, the hats have been blown away from their respective owners. Your task is to rearrange the hats so that no person receives their original hat color. Implement a function rearrange_hats(queue: List[int]) -> List[str] where queue is a list of integers representing the heights of people in the queue and the function returns a list of strings representing the rearranged hat colors. Assume that each person has a hat and there are exactly the same number of people as there are hats.

Example:

Input

queue = [5, 3, 2, 6, 1, 4]

Output

['Blue', 'Green', 'Yellow', 'Red', 'Orange', 'Purple']
from random import shuffle

def rearrange_hats(queue):
hat_colors = ['Red', 'Blue', 'Green', 'Yellow', 'Orange', 'Purple']  # Assuming 6 unique hat colors
shuffle(hat_colors)  # Shuffle the hat colors randomly

    # Map heights to shuffled hat colors
    height_to_color = {height: color for height, color in zip(sorted(queue), hat_colors)}
    
    # Rearrange the hats according to the shuffled colors
    rearranged_hats = [height_to_color[height] for height in queue]
    
    return rearranged_hats

Example usage:

queue = [5, 3, 2, 6, 1, 4]
rearranged_colors = rearrange_hats(queue)
print(rearranged_colors)

This function randomly shuffles the colors and then assigns them to each height in the queue, ensuring no person receives their original hat color.

0

There are 0 answers