
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to Find Out the Points Achievable in a Contest in Python
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)
Example
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))
Input
[600, 400, 1000], [10, 90, 5], 2
Output
392
- Related Articles
- 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
- C++ Program to find maximal achievable diversity of music notes
- Program to find out if k monitoring stations are enough to monitor particular points in Python
- Program to Find Out the Minimal Submatrices 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 Currency Arbitrage in Python
- Program to Find Out the Number of Squares in a Grid in Python
- Program to find out the value of a given equation in Python
- Program to find out the efficient way to study in Python
