Actors and Directors Who Cooperated At Least Three Times — LeetCode 1050 Python Solution
EasyDatabase
- Problem
- #1050
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Report every pair of an actor and a director who have worked together at least three times, in any order. The ActorDirector table has one row per collaboration: actor_id (int), director_id (int) and timestamp (int, the primary key).
Example
ActorDirector table: | actor_id | director_id | timestamp | | -------- | ----------- | --------- | | 5 | 9 | 101 | | 5 | 9 | 102 | | 5 | 9 | 103 | | 5 | 12 | 104 | | 7 | 9 | 105 | Result: | actor_id | director_id | | -------- | ----------- | | 5 | 9 | Actor 5 and director 9 share three rows and qualify, while the other two pairings appear once each.
Python solution
Python
import pandas as pd
def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame:
counts = actor_director.groupby(['actor_id', 'director_id']).size().reset_index(name='n')
return counts[counts['n'] >= 3][['actor_id', 'director_id']]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1050. Actors and Directors Who Cooperated At Least Three Times?
- LeetCode 1050. Actors and Directors Who Cooperated At Least Three Times is rated Easy on LeetCode.
- What topics does LeetCode 1050. Actors and Directors Who Cooperated At Least Three Times cover?
- LeetCode 1050. Actors and Directors Who Cooperated At Least Three Times is tagged Database on LeetCode.