- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 are in a programming contest where there are multiple problems but the contest ends when we solve one problem. Now if we have two lists of numbers of the same length called points, and chances. To explain it, here for the ith problem, we have a chances[i] percent chance of solving it for points[i] points. We also have another value k which represents the number of problems we can attempt. The same problem cannot be attempted twice.

If we devise an optimal strategy, we will have to find the expected value of the number of points we can get in the contest, which is rounded to the nearest integer. We can expect the value of attempting the ith problem as points[i] * chances[i] / 100.0, and this represents the number of points we would get on average.

So, if the input is like points= [600, 400, 1000], chances = [10, 90, 5], k = 2, then the output will be 392.

To solve this, we will follow these steps −

n := size of points

for i in range 0 to n, do

chances[i] := chances[i] / 100.0

R := arrange 0-3 sorted according to points descending

return int(dp(0, K))

Define a function dp() . This will take i, k

if i is same as n, then

return 0.0

j := R[i]

p := chances[j]

ev := p * points[j]

if k is same as 1, then

return maximum of ev, dp(i + 1, k)

return maximum of dp(i + 1, k - 1) *(1 - p) + ev, dp(i + 1, k)

Let us see the following implementation to get better understanding −

class Solution: def solve(self, points, chances, K): n = len(points) for i in range(n): chances[i] /= 100.0 R = sorted(range(n), key=points.__getitem__, reverse=True) def dp(i, k): if i == n: return 0.0 j = R[i] p = chances[j] ev = p * points[j] if k == 1: return max(ev, dp(i + 1, k)) return max(dp(i + 1, k - 1) * (1 - p) + ev, dp(i + 1, k)) return int(dp(0, K)) ob = Solution() print (ob.solve([600, 400, 1000], [10, 90, 5], 2))

[600, 400, 1000], [10, 90, 5], 2

392

- Related Questions & Answers
- Program to find out the maximum points collectable in a game in Python
- Program to Find Out the Maximum Points From Removals in Python
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Program to find out is a point is reachable from the current position through given points in Python
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to find out if k monitoring stations are enough to monitor particular points in Python
- Program to Find Out the Special Nodes in a Tree in Python
- Program to find out the inheritance order in a family in Python
- Program to find out the palindromic borders in a string in python
- Program to find out the value of a given equation in Python
- Program to find out the vertical area between two points where no point lies and is the widest in Python
- Program to Find Out Currency Arbitrage in Python
- Program to Find Out the Number of Squares in a Grid in Python
- Program to find out the efficient way to study in Python

Advertisements