Count Salary Categories — LeetCode 1907 Python Solution
MediumDatabase
- Problem
- #1907
- Reading time
- 5 min
- Source
- leetcode.com
Table schema
SQL
Table: Accounts +-------------+------+ | Column Name | Type | +-------------+------+ | account_id | int | | income | int | +-------------+------+ account_id is the primary key (column with unique values) for this table. Each row contains information about the monthly income for one bank account.Example
SQL
+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id | int |
| income | int |
+-------------+------+
account_id is the primary key (column with unique values) for this table.
Each row contains information about the monthly income for one bank account.Python solution
Python
import duckdb
import pandas as pd
def solution(accounts: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Accounts", accounts)
return con.execute("""WITH
S AS (
SELECT 'Low Salary' AS category
UNION
SELECT 'Average Salary'
UNION
SELECT 'High Salary'
),
T AS (
SELECT
CASE
WHEN income < 20000 THEN "Low Salary"
WHEN income > 50000 THEN 'High Salary'
ELSE 'Average Salary'
END AS category,
COUNT(1) AS accounts_count
FROM Accounts
GROUP BY 1
)
SELECT category, IFNULL(accounts_count, 0) AS accounts_count
FROM
S
LEFT JOIN T USING (category);""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1907. Count Salary Categories?
- LeetCode 1907. Count Salary Categories is rated Medium on LeetCode.
- What topics does LeetCode 1907. Count Salary Categories cover?
- LeetCode 1907. Count Salary Categories is tagged Database on LeetCode.