- Related Questions & Answers
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find minimum number of rocketships needed for rescue in Python
- Find the minimum and maximum amount to buy all N candies in Python
- Program to find minimum distance that needs to be covered to meet all person in Python
- Program to find minimum time to complete all tasks in python
- Program to find the formatted amount of cents of given amount in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to find minimum number of heights to be increased to reach destination in Python
- Program to find minimum bus fare for travelling all days in Python?
- Program to find number of coins needed to make the changes in Python
- Find All Good Strings in C++
- Program to find minimum number of movie theatres required to show all movies in python
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- Program to find minimum number of busses are required to pass through all stops in Python
- Find all good indices in the given Array in Python\n

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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))

[1, 2, 5, 1]

7000

Advertisements