Find the Team Size — LeetCode 1303 Python Solution

EasyLeetCode PremiumDatabase
Problem
#1303
Reading time
3 min

Table schema

SQL
Table: Employee +---------------+---------+ | Column Name | Type | +---------------+---------+ | employee_id | int | | team_id | int | +---------------+---------+ employee_id is the primary key (column with unique values) for this table. Each row of this table contains the ID of each employee and their respective team.

Example

SQL
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| employee_id   | int     |
| team_id       | int     |
+---------------+---------+
employee_id is the primary key (column with unique values) for this table.
Each row of this table contains the ID of each employee and their respective team.

Python solution

Python
import duckdb
import pandas as pd

def solution(employee: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Employee", employee)
    return con.execute("""WITH
    T AS (
        SELECT team_id, COUNT(1) AS team_size
        FROM Employee
        GROUP BY 1
    )
SELECT employee_id, team_size
FROM
    Employee
    JOIN T USING (team_id);""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1303. Find the Team Size?
LeetCode 1303. Find the Team Size is rated Easy on LeetCode.
What topics does LeetCode 1303. Find the Team Size cover?
LeetCode 1303. Find the Team Size is tagged Database on LeetCode.
Is LeetCode 1303. Find the Team Size a premium problem?
Yes. LeetCode 1303. Find the Team Size is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview