← Questions

Order people to maximize happiness from standing before their idol

0
CognitionCodingLast asked Oct 2024

There are n people and each one has an idol, another person in the group. A person earns a given happiness value when they stand to the left of their idol in the line. Return an ordering of everyone that makes the total happiness as large as possible.

Each person points to one idol, so the "idol" links form a graph with n nodes and n edges, and every component holds a cycle: not everyone on a cycle can be satisfied.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding