THE PROBLEM
Imagine the following scenario:
There is a numeric value representing a wattage:
var w = 1000; // 1000 watts
Then there is an array containing a variety of transformers, covering certain wattages:
var transformers = [240, 200, 100, 50, 20];
The goal is to create an array that contains any number of values from transformers
needed to cover for the wattage needs as defined per w
.
However, the resulting array must contain as few elements as possible, thus large numbers come first. However, there must also be minimal loss, so whatever is left over from division, should be covered by the smallest amount (from transformers
) as possible.
The goal is simply to compute the transformers needed to cover for the supplied wattage, with minimal loss (use a smaller transformer when possible, but use the fewest amount of transformers altogether).
EXAMPLE 1
w = 1000;
transformers = [260, 200, 100, 50];
results = [260, 260, 260, 260];
EXAMPLE 2
w = 1042;
transformers = [260, 200, 100, 50];
results = [260, 260, 260, 260, 50];
EXAMPLE 3
w = 502;
transformers = [220, 180, 60, 30];
results = [220, 220, 180];
MY APPROACH
Of course i tried to solve this myself, but due to a lack of mathematical computing capabilities of my brain, i failed miserably.
My approach was this:
- Create an array that contains the available wattages, sorted in descending order
- Loop over a total wattage requirement (a copy of
w
) until the total wattage is 0 or lower - Divide the total wattage requirement by the largest transformer available
- If division result is larger than 1, push the current transformer to the results array and subtract the current wattage from the total wattage
- If division result is smaller than 1, shift the array of transformers to remove the largest transformer in the array
- If array of transformers has only one element left, push that to the results array and set the total wattage to 0
The results came close to correct, but in some cases turned out to be wrong as more optimal choices for covering the wattage could have been made. I also had cases when there was uncovered for wattage left.
My Code
// test values:
var wattages = [264, 100, 60, 35, 18];
var _wattageTotal = 1000; // 1000 watts
var _trafos=new Array(); // for the results
console.log('-----------------------');
console.log('THESE ARE THE AVAILABLE WATTAGES:');
console.log(wattages);
while(_wattageTotal){
console.log('WATTAGE TOTAL: '+_wattageTotal);
console.log('TESTING: '+wattages[0]+' against total wattage => '+_wattageTotal/wattages[0]);
if( _wattageTotal/wattages[0] >= 1 ) {
console.log('==> FOUND FIT: '+wattages[0]);
_trafos.push( byWattage[ wattages[0] ].key );
_wattageTotal-=wattages[0];
} else {
console.log(wattages[0]+' DOES NOT FIT');
wattages.shift();
if(wattages.length==1){
_trafos.push( byWattage[ wattages[0] ].key );
_wattageTotal=0;
}
}
}
don't set
_wattageTotal
to0
. When you do it exits the while loop, and any leftover not covered by your current_trafos
will be ignoredyou also need to shift the current wattage after performing the check for
length =1
, and check for _wattageTotal > 0 in your while loop:jsfiddle