Calculate Salaries — LeetCode 1468 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1468
Reading time
5 min

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

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(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.

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