#2880
Select Data
EasyHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
In the optimal solution, we can leverage the capabilities of pandas to filter the DataFrame directly. This approach is efficient and concise, allowing us to avoid manual iteration and condition checking.
⚙️
Algorithm
3 steps- 1Step 1: Use the DataFrame's filtering capabilities to select rows where student_id is 101.
- 2Step 2: Select the 'name' and 'age' columns from the filtered DataFrame.
- 3Step 3: Return the resulting DataFrame.
solution.py4 lines
1import pandas as pd
2
3def select_student(dataframe):
4 return dataframe[dataframe['student_id'] == 101][['name', 'age']]ℹ
Complexity note: The time complexity remains O(n) as we still need to check each student. The space complexity is O(n) because we create a new DataFrame or list to hold the filtered results.
- 1Using built-in DataFrame methods can greatly simplify code.
- 2Filtering and selecting columns in one step is efficient.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.