Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to Optimize the function

I'm working on this function that has to return all possible values of adding a and b n times for example if n = 1 then possible values would be a + a a + b and b + b. function below works but it's too slow and I want to optimize it. Any suggestions? Thanks a lot!

function processData(n, a, b){
  var ans = [0];
  for(var i = 0; i < n; i++){
    var temp = [];
    for(var j = 0; j < ans.length; j++){
      var aa = ans[j] + a;
      if(temp.includes(aa) === false){
       temp.push(aa);
      }
      var bb = ans[j] + b;
      if(temp.includes(bb) === false){
        temp.push(bb);
      }
    }
    ans = temp;
  }
  ans.sort(function(a, b){return a - b});
  return ans;
}
like image 780
brigysl Avatar asked Nov 18 '16 16:11

brigysl


People also ask

What does it mean to optimize a function?

Mathematically speaking, optimization is the minimization or maximization of a function subject to constraints on its variables.

Why do we need to optimize?

The purpose of optimization is to achieve the “best” design relative to a set of prioritized criteria or constraints. These include maximizing factors such as productivity, strength, reliability, longevity, efficiency, and utilization.

How do you find the optimization of a function?

To solve an optimization problem, begin by drawing a picture and introducing variables. Find an equation relating the variables. Find a function of one variable to describe the quantity that is to be minimized or maximized. Look for critical points to locate local extrema.

What does it mean to optimize something in calculus?

Optimization is the process of finding maximum and minimum values given constraints using calculus. For example, you'll be given a situation where you're asked to find: The Maximum Profit. The Minimum Travel Time. Or Possibly The Least Costly Enclosure.


3 Answers

function processData(n, a, b) {
  var ans = [];
  if (a == b) {
    for (var i=0; i<n+1; i++) {
      ans.push(a * n);
    }
    return ans;
  } else if (a > b) {
    var temp = a;
    a = b;
    b = temp;
  }
  var diff = b - a;
  for (var i=0; i<n+1; i++) {
      ans.push(a * n + diff * i);
  }
  return ans;
}

Okay, this is by far the most efficient solution. I've just tested it on fiddle.

All other three solutions greatly outperform yours. Mine is better than @abc123's because there's no need for sorting and better than @georg's because there's no need for using set or sorting.

like image 143
Terry Li Avatar answered Nov 12 '22 15:11

Terry Li


This is a proposal which gets the postions for a and b.

function combine(left, right) {

    function carry() {
        return c.reduceRight(function (r, _, i, o) {
            return r && !(o[i] = (o[i] + 1) % left.length);
        }, 1);
    }

    var c = Array.apply(null, { length: right.length }).map(function () { return 0; }),
        result = [];

    do {
        result.push(c.reduce(function (r, a, i) {
            r[left[a]].push(right[i]);
            return r;
        }, left.reduce(function (r, a) {
            r[a] = [];
            return r;
        }, {})));
    } while (!carry());
    return result;
}

console.log(combine(['a', 'b'], [1, 2, 3]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Just to make a disclosure, the answer is one of mine, given to a more detailed question of kind of the same, but not exactly: Find all combinations of two arrays

like image 36
Nina Scholz Avatar answered Nov 12 '22 15:11

Nina Scholz


Here is a concise ES6 function for it:

function processData(n, a, b) {
    let diff = Math.abs(b-a), sum = Math.min(a,b) * (n+1) - diff;
    return diff ? Array.from(Array(n+2), _ => sum += diff) : [sum]; 
}

console.log(processData(2, 3, 5));
like image 2
trincot Avatar answered Nov 12 '22 14:11

trincot