HardNo. 2010LeetCode Premium
The Number of Seniors and Juniors to Join the Company II — LeetCode 2010 Python Solution
- Topics
- Database
- Reading time
- 7 min
- Source
- leetcode.com
Table 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
+-------------+------+
| 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
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
| 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 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.