- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find minimum amount needed to be paid all good performers in Python
Suppose we have given a list of numbers called ratings, and this is showing the performance scores of coders. Now the manager wants to give Rs 1000 to every coder except if two coders are adjacent, they would like to pay the better performing coder at least Rs 1000 higher than the worse performing one. We have to find the minimum amount the manager can pay following above constraints.
So, if the input is like ratings = [1, 2, 5, 1], then the output will be 7000, as the minimum we can pay for each respective coder is [1000, 2000, 3000, 1000]
To solve this, we will follow these steps −
pay:= a list of size same as ratings, initially all values are 1
for i in range 1 to size of ratings - 1, do
if ratings[i] > ratings[i-1], then
pay[i] := pay[i-1]+1
for i in range size of ratings -2 to 0, decrease by 1, do
if ratings[i] > ratings[i+1], then
pay[i] := maximum of pay[i] and pay[i+1]+1
return (sum of the elements of pay) *1000
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, ratings): pay=[1 for _ in ratings] for i in range(1, len(ratings)): if ratings[i] > ratings[i-1]: pay[i] = pay[i-1]+1 for i in range(len(ratings)-2,-1,-1): if ratings[i] > ratings[i+1]: pay[i] = max(pay[i], pay[i+1]+1) return sum(pay)*1000 ob = Solution() ratings = [1, 2, 5, 1] print(ob.solve(ratings))
Input
[1, 2, 5, 1]
Output
7000
- Related Articles
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find final amount that should be paid to employees based on their performance in C++
- Program to find minimum element addition needed to get target sum in Python
- Program to find minimum number of rocketships needed for rescue in Python
- Program to find minimum costs needed to fill fruits in optimized way in Python
- Find the minimum and maximum amount to buy all N candies in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- Program to find minimum jump needed to return from a folder to home in Python
- C++ code to find minimum time needed to do all tasks
- Program to find minimum distance that needs to be covered to meet all person in Python
- C++ program to find out the minimum amount of time needed to reach from source to destination station by train
- Program to find minimum number of swaps needed to arrange all pair of socks together in C++
- Program to find minimum time to complete all tasks in python
- Program to find minimum cost to connect all points in Python
- Program to find minimum time to finish all jobs in Python
