The Number of Seniors and Juniors to Join the Company II — LeetCode 2010 Python Solution

HardLeetCode PremiumDatabase
Problem
#2010
Reading time
7 min

Table schema

SQL
Table: Candidates +-------------+------+ | Column Name | Type | +-------------+------+ | employee_id | int | | experience | enum | | salary | int | +-------------+------+ employee_id is the column with unique values for this table. experience is an ENUM (category) of types ('Senior', 'Junior').

Example

SQL
+-------------+------+
| Column Name | Type |
+-------------+------+
| employee_id | int  |
| experience  | enum |
| salary      | int  |
+-------------+------+
employee_id is the column with unique values for this table.
experience is an ENUM (category) of types ('Senior', 'Junior').
Each row of this table indicates the id of a candidate, their monthly salary, and their experience.
The salary of each candidate is guaranteed to be unique.

Python solution

Python
import duckdb
import pandas as pd

def solution(candidates: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Candidates", candidates)
    return con.execute("""WITH
    s AS (
        SELECT
            employee_id,
            SUM(salary) OVER (ORDER BY salary) AS cur
        FROM Candidates
        WHERE experience = 'Senior'
    ),
    j AS (
        SELECT
            employee_id,
            IFNULL(
                SELECT
                    MAX(cur)
                FROM s
                WHERE cur <= 70000,
                0
            ) + SUM(salary) OVER (ORDER BY salary) AS cur
        FROM Candidates
        WHERE experience = 'Junior'
    )
SELECT
    employee_id
FROM s
WHERE cur <= 70000
UNION
SELECT
    employee_id
FROM j
WHERE cur <= 70000;""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 2010. The Number of Seniors and Juniors to Join the Company II?
LeetCode 2010. The Number of Seniors and Juniors to Join the Company II is rated Hard on LeetCode.
What topics does LeetCode 2010. The Number of Seniors and Juniors to Join the Company II cover?
LeetCode 2010. The Number of Seniors and Juniors to Join the Company II is tagged Database on LeetCode.
Is LeetCode 2010. The Number of Seniors and Juniors to Join the Company II a premium problem?
Yes. LeetCode 2010. The Number of Seniors and Juniors to Join the Company II 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