MediumNo. 2159LeetCode Premium
Order Two Columns Independently — LeetCode 2159 Python Solution
- Topics
- Database
- Reading time
- 4 min
- Source
- leetcode.com
Table schema
Table: Data +-------------+------+ | Column Name | Type | +-------------+------+ | first_col | int | | second_col | int | +-------------+------+ This table may contain duplicate rows. Write a solution to independently: order first_col in ascending order.Example
+-------------+------+
| Column Name | Type |
+-------------+------+
| first_col | int |
| second_col | int |
+-------------+------+
This table may contain duplicate rows.Python solution
import duckdb
import pandas as pd
def solution(data: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Data", data)
return con.execute("""WITH
S AS (
SELECT
first_col,
ROW_NUMBER() OVER (ORDER BY first_col) AS rk
FROM Data
),
T AS (
SELECT
second_col,
ROW_NUMBER() OVER (ORDER BY second_col DESC) AS rk
FROM Data
)
SELECT first_col, second_col
FROM
S
JOIN T USING (rk);""").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 2159. Order Two Columns Independently?
LeetCode 2159. Order Two Columns Independently is rated Medium on LeetCode.
- What topics does LeetCode 2159. Order Two Columns Independently cover?
LeetCode 2159. Order Two Columns Independently is tagged Database on LeetCode.
- Is LeetCode 2159. Order Two Columns Independently a premium problem?
Yes. LeetCode 2159. Order Two Columns Independently is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.