EasyNo. 1050
Actors and Directors Who Cooperated At Least Three Times — LeetCode 1050 Python Solution
- Topics
- Database
- 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
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
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
| 0178 | Rank Scores | Medium |
| 0180 | Consecutive Numbers | Medium |
| 0181 | Employees Earning More Than Their Managers | Easy |
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.