HardNo. 0579LeetCode Premium
Find Cumulative Salary of an Employee — LeetCode 579 Python Solution
- Topics
- Database
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
Table: Employee +-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | month | int | | salary | int | +-------------+------+ (id, month) is the primary key (combination of columns with unique values) for this table. Each row in the table indicates the salary of an employee in one month during the year 2020.Example
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| month | int |
| salary | int |
+-------------+------+
(id, month) is the primary key (combination of columns with unique values) for this table.
Each row in the table indicates the salary of an employee in one month during the year 2020.Python solution
import pandas as pd
def cumulative_salary(employee: pd.DataFrame) -> pd.DataFrame:
df = employee.sort_values(['id', 'month'])
df['rolling'] = (
df.groupby('id')['salary']
.rolling(3, min_periods=1)
.sum()
.reset_index(level=0, drop=True)
)
max_month = df.groupby('id')['month'].transform('max')
res = df[df['month'] < max_month][['id', 'month', 'rolling']]
res = res.rename(columns={'rolling': 'salary'})
return resComplexity
- Time
- O(n log n)(typical)
- Space · auxiliary
- O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0577 | Employee Bonus | Easy |
| 0584 | Find Customer Referee | Easy |
| 0585 | Investments in 2016 | Medium |
| 0586 | Customer Placing the Largest Number of Orders | Easy |
| 0570 | Managers with at Least 5 Direct Reports | Medium |
| 0175 | Combine Two Tables | Easy |
Frequently asked questions
- How hard is LeetCode 579. Find Cumulative Salary of an Employee?
LeetCode 579. Find Cumulative Salary of an Employee is rated Hard on LeetCode.
- What topics does LeetCode 579. Find Cumulative Salary of an Employee cover?
LeetCode 579. Find Cumulative Salary of an Employee is tagged Database on LeetCode.
- Is LeetCode 579. Find Cumulative Salary of an Employee a premium problem?
Yes. LeetCode 579. Find Cumulative Salary of an Employee is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.