EasyNo. 0627
Swap Sex of Employees — LeetCode 627 Python Solution
- Topics
- Database
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Flip the sex column of every row in place, turning every m into an f and every f into an m, with a single update statement and no temporary table. The answer is the Salary table itself once the update has run. The Salary table has one row per employee: id (int, the primary key), name (varchar), sex (an enum whose only values are 'm' and 'f') and salary (int).
Example
Salary table: | id | name | sex | salary | | -- | ----- | --- | ------ | | 1 | Lena | f | 5200 | | 2 | Marco | m | 4800 | | 3 | Nia | f | 6100 | | 4 | Omar | m | 4400 | Salary table after the update: | id | name | sex | salary | | -- | ----- | --- | ------ | | 1 | Lena | m | 5200 | | 2 | Marco | f | 4800 | | 3 | Nia | m | 6100 | | 4 | Omar | f | 4400 | Every value in the sex column is replaced by its opposite in one pass, and no other column moves.
Python solution
import pandas as pd
def swap_sex(salary: pd.DataFrame) -> pd.DataFrame:
df = salary.copy()
df['sex'] = df['sex'].map(lambda x: 'f' if x == 'm' else 'm')
return dfComplexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0626 | Exchange Seats | Medium |
| 0620 | Not Boring Movies | Easy |
| 0619 | Biggest Single Number | Easy |
| 0175 | Combine Two Tables | Easy |
| 0176 | Second Highest Salary | Medium |
| 0177 | Nth Highest Salary | Medium |
Frequently asked questions
- How hard is LeetCode 627. Swap Sex of Employees?
LeetCode 627. Swap Sex of Employees is rated Easy on LeetCode.
- What topics does LeetCode 627. Swap Sex of Employees cover?
LeetCode 627. Swap Sex of Employees is tagged Database on LeetCode.