HardNo. 2010LeetCode Premium

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

Topics
Database
Reading time
7 min

Table schema

SQLTable schema
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

SQLExample
+-------------+------+
| 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

PythonSolution
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

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

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.

Free to download. Check it runs on your machine, then pick a plan.