Skip to content

Commit 4d53ec5

Browse files
committed
Sync LeetCode submission Runtime - 489 ms (34.59%), Memory - 59.7 MB (100.00%)
1 parent 8a087b6 commit 4d53ec5

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

0177-nth-highest-salary/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>Table: <code>Employee</code></p>
2+
3+
<pre>
4+
+-------------+------+
5+
| Column Name | Type |
6+
+-------------+------+
7+
| id | int |
8+
| salary | int |
9+
+-------------+------+
10+
id is the primary key (column with unique values) for this table.
11+
Each row of this table contains information about the salary of an employee.
12+
</pre>
13+
14+
<p>&nbsp;</p>
15+
16+
<p>Write a solution to find the <code>n<sup>th</sup></code> highest salary from the <code>Employee</code> table. If there is no <code>n<sup>th</sup></code> highest salary, return&nbsp;<code>null</code>.</p>
17+
18+
<p>The result format is in the following example.</p>
19+
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong>
25+
Employee table:
26+
+----+--------+
27+
| id | salary |
28+
+----+--------+
29+
| 1 | 100 |
30+
| 2 | 200 |
31+
| 3 | 300 |
32+
+----+--------+
33+
n = 2
34+
<strong>Output:</strong>
35+
+------------------------+
36+
| getNthHighestSalary(2) |
37+
+------------------------+
38+
| 200 |
39+
+------------------------+
40+
</pre>
41+
42+
<p><strong class="example">Example 2:</strong></p>
43+
44+
<pre>
45+
<strong>Input:</strong>
46+
Employee table:
47+
+----+--------+
48+
| id | salary |
49+
+----+--------+
50+
| 1 | 100 |
51+
+----+--------+
52+
n = 2
53+
<strong>Output:</strong>
54+
+------------------------+
55+
| getNthHighestSalary(2) |
56+
+------------------------+
57+
| null |
58+
+------------------------+
59+
</pre>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pandas as pd
2+
3+
def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame:
4+
df = employee[['salary']].drop_duplicates()
5+
if len(df) < N:
6+
return pd.DataFrame({f'getNthHighestSalary({N})' : [None]})
7+
return df.sort_values('salary', ascending=False).head(N).tail(1)[['salary']]

0 commit comments

Comments
 (0)