#2884
Modify Columns
EasyDataFrame manipulationVectorized operations
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n) | O(n) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(n)Space O(1)
This approach leverages built-in DataFrame operations to modify the salary column directly, which is efficient and concise. It avoids explicit loops in favor of vectorized operations.
⚙️
Algorithm
2 steps- 1Step 1: Use the DataFrame's built-in functionality to multiply the salary column by 2 directly.
- 2Step 2: Return the modified DataFrame.
solution.py9 lines
1import pandas as pd
2
3def modify_salaries(employees):
4 employees['salary'] *= 2
5 return employees
6
7# Example usage
8employees = pd.DataFrame({'name': ['Jack', 'Piper', 'Mia', 'Ulysses'], 'salary': [19666, 74754, 62509, 54866]})
9print(modify_salaries(employees))ℹ
Complexity note: The time complexity remains O(n) because we still iterate through the list of employees, but we do it in a more efficient manner. The space complexity is O(1) as we modify the salaries in place.
- 1Understanding how to manipulate DataFrames is crucial for data-related tasks.
- 2Vectorized operations in libraries like pandas are often more efficient than manual loops.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.