Calculate Salaries — LeetCode 1468 Python Solution
MediumLeetCode PremiumDatabase
- Problem
- #1468
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
SQL
Table Salaries: +---------------+---------+ | Column Name | Type | +---------------+---------+ | company_id | int | | employee_id | int | | employee_name | varchar | | salary | int | +---------------+---------+ In SQL,(company_id, employee_id) is the primary key for this table. This table contains the company id, the id, the name, and the salary for an employee.Example
SQL
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| company_id | int |
| employee_id | int |
| employee_name | varchar |
| salary | int |
+---------------+---------+
In SQL,(company_id, employee_id) is the primary key for this table.
This table contains the company id, the id, the name, and the salary for an employee.Python solution
Python
import duckdb
import pandas as pd
# Pass input tables as keyword arguments matching the SQL table names.
def solution(**tables) -> pd.DataFrame:
con = duckdb.connect()
for name, df in tables.items():
con.register(name, df)
return con.execute("""SELECT
s.company_id,
employee_id,
employee_name,
ROUND(
CASE
WHEN top < 1000 THEN salary
WHEN top >= 1000
AND top <= 10000 THEN salary * 0.76
ELSE salary * 0.51
END
) AS salary
FROM
Salaries AS s
JOIN (
SELECT company_id, MAX(salary) AS top
FROM Salaries
GROUP BY company_id
) AS t
ON s.company_id = t.company_id;""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1468. Calculate Salaries?
- LeetCode 1468. Calculate Salaries is rated Medium on LeetCode.
- What topics does LeetCode 1468. Calculate Salaries cover?
- LeetCode 1468. Calculate Salaries is tagged Database on LeetCode.
- Is LeetCode 1468. Calculate Salaries a premium problem?
- Yes. LeetCode 1468. Calculate Salaries is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.