From 10219b25fdeb24cf95ba94198bd6b035ee2f75d2 Mon Sep 17 00:00:00 2001 From: Sangeeth Santhosh <73825180+sangeeths29@users.noreply.github.com> Date: Thu, 12 Jun 2025 21:42:45 -0700 Subject: [PATCH] Add files via upload --- 01-NthHighestSalary.py | 13 +++++++++++++ 02-SecondHighestSalary.py | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 01-NthHighestSalary.py create mode 100644 02-SecondHighestSalary.py diff --git a/01-NthHighestSalary.py b/01-NthHighestSalary.py new file mode 100644 index 0000000..0fdaf4a --- /dev/null +++ b/01-NthHighestSalary.py @@ -0,0 +1,13 @@ +# Problem 1 - Nth Highest Salary (https://leetcode.com/problems/nth-highest-salary/solution/) +import pandas as pd + +def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: + # Dropping duplicate salaries from table + df = employee[['salary']].drop_duplicates() + + # Returning null if N is greater than number of salaries in table or if N is less than or equal to 0 + if N > len(df) or N <= 0: + return pd.DataFrame({f'getNthHighestSalary({N})' : [None]}) + + # Ordering salaries in descending order and returning the N top ones. The tail function filters out the distinct Nth highest salary + return df.sort_values('salary', ascending = False).head(N).tail(1)[['salary']].rename(columns = {'salary':f'getNthHighestSalary({N})'}) \ No newline at end of file diff --git a/02-SecondHighestSalary.py b/02-SecondHighestSalary.py new file mode 100644 index 0000000..3cc96a8 --- /dev/null +++ b/02-SecondHighestSalary.py @@ -0,0 +1,11 @@ +# Problem 2 - Second Highest Salary ( https://leetcode.com/problems/second-highest-salary/ ) +import pandas as pd + +def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: + # Dropping duplicate salary values + df = employee[['salary']].drop_duplicates() + # Returning None if number of salaries is less than 2 + if len(df) < 2: + return pd.DataFrame({'SecondHighestSalary' : [None]}) + # Returning 2nd Highest Salary + return df.sort_values('salary', ascending = False).head(2).tail(1)[['salary']].rename(columns = {'salary':'SecondHighestSalary'})