top of page

Actors and Directors Who Cooperated At Least Three Times

LeetCode Problem 1050

Actors and Directors Who Cooperated At Least Three Times
Write a solution to find all the pairs (actor_id, director_id) where the actor has cooperated with the director at least three times. Return the result table in any order.

import pandas as pd

data = [[1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2, 3], [1, 2, 4], [2, 1, 5], [2, 1, 6]]
actor_director = pd.DataFrame(data, columns=['actor_id', 'director_id', 'timestamp']).astype({'actor_id':'int64', 'director_id':'int64', 'timestamp':'int64'})


def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame:
    grouped = actor_director.groupby(['actor_id', 'director_id']).size().reset_index(name='cooperation_count')
    filtered_pairs = grouped[grouped['cooperation_count'] >= 3]
    return filtered_pairs[['actor_id', 'director_id']]

result = actors_and_directors(actor_director)
print(result)

bottom of page