Calculate the weight required for the barbell to get a specific weight

I am trying to figure out the optimal set of cymbals to put on the barbell to get to the right weight. It works well, but the problem is that it will always find the first possible weight under the target weight.

For example, if you want your barbell to weigh 29kg

, you can place 27.5kg

or 30kg

given a set of tables below, where each plate counts as a pair. When you get to know these two options, you would choose 30kg

, since it is closer to 29

than 27.5

.

In this example, I just have a calculation 27.5

, but I didn't figure out how to go back and calculate the maximum possible weight after the if statement fails.

const BAR = 20;

const PLATES = [
  1.25,
  2.5,
  2.5,
  5,
  5,
  10,
  10,
  20,
  20,
];

const sumPlates = (plates) => {
  return plates.reduce((acc, plate) => {
    return acc + (plate * 2);
  }, 0);
};

const rack = (targetWeight) => {
  const sortedPlates = PLATES.sort((a, b) => b - a);

  const rackedPlates = sortedPlates.reduce((acc, plate) => {
    if ((BAR + (plate * 2) + sumPlates(acc)) > targetWeight) {
      // Calculate here the closest possible rack weight
      return acc;
    }

    acc.push(plate);

    return acc;
  }, []);

  return {
    targetWeight,
    barbellWeight: BAR + sumPlates(rackedPlates),
    plates: rackedPlates,
  };
};

console.log(rack(47)); // gives 45 but should give 47.5
console.log(rack(29)); // gives 27.5 but should give 30

      

+3


source to share


1 answer


This is actually a modified version of the Change Task , which itself is a modified version of the Knapsack problem.

Depending on the size and type of the given weight, you can use the dynamic programming solution in the link to find all the possible combinations, and iterate O(N)

to find the closest to the target weight.




However, given your current implementation, I would simply do the following:

var x = rack(targetSum);
var y = rack(2*targetSum - x);
ans = the closer one to targetSum 

      

+1


source







All Articles