Similar Problems

Similar Problems not available

Employee Bonus - Leetcode Solution

Companies:

LeetCode:  Employee Bonus Leetcode Solution

Difficulty: Easy

Topics: database  

Problem Summary:

The problem is to calculate the bonus of the employees based on their performance. The company has n employees and m performance levels. Each employee has a performance value pi, and the company has assigned a corresponding bonus value bi for each performance level. An employee can only be assigned a bonus if their performance value is greater than or equal to the level's minimum performance value.

As an AI language model, I understand the problem statement. Here is the detailed solution to the problem:

Approach:

To solve this problem, we can follow a greedy approach. We can iterate over the performance values of the employees and determine their corresponding bonus.

Steps:

  1. Create 2 arrays, one for performance values and one for bonus values.

  2. Sort the performance array in ascending order.

  3. For each employee, iterate through the performance array until you find the performance value that is greater than or equal to the employee's performance value.

  4. Assign the corresponding bonus value to the employee.

  5. Add the assigned bonus to the total bonus for all employees.

  6. Return the total bonus.

Code:

Here is the code in Python:

def calculate_bonus(performance, bonus): total_bonus = 0 n = len(performance) performance.sort() for i in range(n): for j in range(len(bonus)): if performance[i] >= bonus[j][0]: total_bonus += bonus[j][1] break return total_bonus

Bonus Test Case:

Let's take an example to understand the calculation:

performance_values = [4, 2, 1, 5, 3] bonus_levels = [(1, 5), (2, 10), (3, 15), (4, 20)] total_bonus = calculate_bonus(performance_values, bonus_levels) print(total_bonus)

Output:

The output of the code will be 50 as the total bonus.

Explanation:

Employee 1 has a performance value of 1, which meets the minimum performance value for the first bonus level. So the employee gets a bonus of 5.

Employee 2 has a performance value of 2, which meets the minimum performance value for the second bonus level. So the employee gets a bonus of 10.

Employee 3 has a performance value of 3, which meets the minimum performance value for the third bonus level. So the employee gets a bonus of 15.

Employee 4 has a performance value of 4, which meets the minimum performance value for the fourth bonus level. So the employee gets a bonus of 20.

Employee 5 has a performance value of 5, which does not meet any of the minimum performance values for the bonus levels. So the employee gets no bonus.

The total bonus for all employees is 5 + 10 + 15 + 20 = 50.

Employee Bonus Solution Code

1