Find the Nth Highest Salary – Clean & Efficient Approach! 🚀
IntuitionWhen asked to find the N-th highest salary, the key challenge is that salaries may have duplicates, so we can't simply pick the N-th row from a sorted


## **Intuition**  
When asked to find the **N-th highest salary**, the key challenge is that salaries may have duplicates, so we can't simply pick the N-th row from a sorted list.  

1. **Understanding the Problem:**  
   - If there are **multiple employees with the same salary**, we should **consider unique salaries** when ranking.
   - If `N` is **larger than the number of unique salaries**, we should return `null` because there isn't an N-th highest salary.
   - If `N` is **zero or negative**, it's an invalid input, and we should also return `null`.  

2. **Example to Build Intuition:**  
   Let's say we have the following employee salary table:  

   | id | salary |
   |----|--------|
   | 1  | 3000   |
   | 2  | 2000   |
   | 3  | 3000   |
   | 4  | 1000   |

   - **Unique salaries:** `[3000, 2000, 1000]` (sorted in descending order).
   - **1st highest salary** → `3000`  
   - **2nd highest salary** → `2000`  
   - **3rd highest salary** → `1000`  
   - **4th highest salary** → `null` (since we only have 3 unique values).  



## **Approach**  
To solve this problem step by step:  

1. **Extract Unique Salaries**  
   - Use `.drop_duplicates()` to remove duplicate salary values.
   - Sort them in **descending** order to rank the highest salaries first.

2. **Handle Edge Cases**  
   - If `N <= 0`, return `null` (invalid input).
   - If `N` is **greater than the number of unique salaries**, return `null`.

3. **Retrieve the N-th Highest Salary**  
   - Use `.iloc[N-1]` to get the correct index (since Python uses **zero-based indexing**).

4. **Format the Output Correctly**  
   - Return the result in a Pandas DataFrame with the correct column name.


### **Time Complexity**  
- **Removing Duplicates (`drop_duplicates()`)** → **O(n)**  
- **Sorting Salaries (`sort_values()`)** → **O(n log n)**  
- **Indexing into the Sorted List (`iloc[N-1]`)** → **O(1)**  
- **Overall Time Complexity**: **O(n log n)** (dominated by sorting)

### **Space Complexity**  
- We only store a sorted list of unique salaries → **O(n)**
- The output DataFrame is **O(1)** in size since we return a single value.  
- **Overall Space Complexity**: **O(n)** (due to storing unique salaries)

![upvote.jpeg](https://assets.leetcode.com/users/images/a4e005c6-ba2f-42c4-a441-0ea957771cda_1740915822.0978057.jpeg)


# Code
```pythondata []
import pandas as pd

def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame:
    # Get unique salaries sorted in descending order
    unique_salaries = employee['salary'].drop_duplicates().sort_values(ascending=False)

    # If N is invalid (negative or zero), return "null" as a string
    if N <= 0:
        return pd.DataFrame({f'getNthHighestSalary({N})': [None]})

    # If N is larger than the number of unique salaries, return None
    if len(unique_salaries) < N:
        return pd.DataFrame({f'getNthHighestSalary({N})': [None]})

    # Get the N-th highest salary
    nth_salary = unique_salaries.iloc[N-1]

    return pd.DataFrame({f'getNthHighestSalary({N})': [nth_salary]})



```
['Pandas']