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'})