Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to Find Out the Points Achievable in a Contest in Python
In a programming contest where we can attempt multiple problems but the contest ends when we solve one, we need to find the optimal strategy to maximize expected points. Given two lists points and chances of equal length, where chances[i] represents the percentage chance of solving problem i for points[i] points, and k representing the maximum problems we can attempt.
The expected value of attempting the ith problem is points[i] * chances[i] / 100.0. We need to find the optimal strategy that maximizes expected points.
Algorithm Approach
We use dynamic programming with the following strategy:
Convert chances to probabilities by dividing by 100
Sort problems by points in descending order to prioritize high−value problems
Use recursive function to calculate maximum expected value
At each step, choose between attempting current problem or skipping it
Example
Let's implement the solution with a concrete example:
class Solution:
def solve(self, points, chances, k):
n = len(points)
# Convert percentages to probabilities
for i in range(n):
chances[i] /= 100.0
# Sort indices by points in descending order
sorted_indices = sorted(range(n), key=points.__getitem__, reverse=True)
def dp(i, remaining_attempts):
# Base case: no more problems to consider
if i == n:
return 0.0
current_problem = sorted_indices[i]
probability = chances[current_problem]
expected_value = probability * points[current_problem]
# If only one attempt left, choose max between current and next
if remaining_attempts == 1:
return max(expected_value, dp(i + 1, remaining_attempts))
# Choose max between:
# 1. Attempting current problem
# 2. Skipping current problem
attempt_current = dp(i + 1, remaining_attempts - 1) * (1 - probability) + expected_value
skip_current = dp(i + 1, remaining_attempts)
return max(attempt_current, skip_current)
return int(dp(0, k))
# Test the solution
solution = Solution()
result = solution.solve([600, 400, 1000], [10, 90, 5], 2)
print(f"Maximum expected points: {result}")
Maximum expected points: 392
How It Works
The algorithm works by:
Sorting by value: Problems are sorted by points in descending order to prioritize high−reward problems
-
Dynamic programming: For each problem, we calculate the expected value of two strategies:
- Attempting the problem: Expected value + probability of continuing to next problems
- Skipping the problem: Move to next problem with same number of attempts
Optimal choice: We choose the strategy that gives maximum expected points
Step−by−Step Breakdown
For the example points=[600, 400, 1000], chances=[10, 90, 5], k=2:
# After sorting by points: [1000, 600, 400] with chances [5%, 10%, 90%]
# Problem priorities: index 2 (1000 pts, 5%), index 0 (600 pts, 10%), index 1 (400 pts, 90%)
points = [600, 400, 1000]
chances = [10, 90, 5]
# Convert to probabilities
prob = [c/100.0 for c in chances]
print(f"Probabilities: {prob}")
# Expected values
expected = [p * pts for p, pts in zip(prob, points)]
print(f"Expected values: {expected}")
# Sort by points descending
sorted_data = sorted(zip(points, prob), reverse=True)
print(f"Sorted by points: {sorted_data}")
Probabilities: [0.1, 0.9, 0.05] Expected values: [60.0, 360.0, 50.0] Sorted by points: [(1000, 0.05), (600, 0.1), (400, 0.9)]
Conclusion
The solution uses dynamic programming to find the optimal contest strategy by considering all possible problem−solving sequences. It prioritizes high−value problems while accounting for success probabilities, returning the maximum expected points rounded to the nearest integer.
