BackhardGreedy

Optimizing Rational Selection Solution

Problem Statement

Given a set of rational numbers, each defined as a pair of numerator and denominator, and a capacity constraint, select a subset of these rationals to maximize the total value. The value of each fraction is determined by its magnitude (numerator/denominator). The total value of the selected fractions should not exceed the given capacity.

Example 1
Input
[[3, 4], [5, 6], [7, 8]] with capacity 1.5
Output
3.25

Explanation: Select the fractions 5/6 and 3/4 to achieve the maximum value within the given capacity.

Example 2
Input
[[1, 2], [1, 3], [2, 3]] with capacity 1
Output
1.83333333333

Explanation: Choose the fraction 2/3, then select 1/2 and 1/3 in proportion to fill the remaining capacity, maximizing the total value to 1.83333333333.

Constraints

  • 1 <= number of rational numbers <= 100
  • Each numerator and denominator is in the range [1, 1000].
  • 1 <= capacity <= 1000
  • Denominators are non-zero and fractions are in simplest form.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free