简体   繁体   English

如何优化我的JavaScript程序?

[英]How can I optimize my javascript program?

I am trying to solve this javascript problem and came up with this solution which gives the correct solution but the online judge is showing time limit exceeded. 我正在尝试解决此javascript问题,并提出了此解决方案,该解决方案提供了正确的解决方案,但在线判断显示超出了时间限制。 I want to optimize the algorithm. 我想优化算法。 What can I change in my algorithm? 我的算法可以更改什么?

Problem Description: The special score(ssc) of an array of integers will be the sum of each integer multiplied by its corresponding index plus one in the array. 问题描述:整数数组的特殊分数(ssc)将是每个整数的总和乘以其对应的索引再加上一个整数。

Eg: with the 例如:随着

array [6, 12, -1]

arr =   [6,      12,       -1 ]
ssc =   1*6  +  2* 12 +  3.(*1) = 6 + 24 - 3 = 27

The array given in the example has six(6) permutations and are with the corresponding ssc: 示例中给出的数组具有六个(6)排列,并具有相应的ssc:

Permutations Special Score (ssc) [6, 12, -1] 1*6 + 2*12 + 3*(-1) = 27 [6, -1, 12] 1*6 + 2*(-1) + 3*12 = 40 [-1, 6, 12] 1*(-1) + 2*6 + 3*12 = 47 [-1, 12, 6] 1*(-1) + 2*12 + 3*6 = 41 [12, -1, 6] 1*12 + 2*(-1) + 3*6 = 28 [12, 6, -1] 1*12 + 2*6 + 3*(-1) = 21 The total sum of the ssc's of all the possible permutations is: 27 + 40 + 47 + 41 + 28 + 21 = 204 排列特殊分数(ssc)[6,12,-1] 1 * 6 + 2 * 12 + 3 *(-1)= 27 [6,-1,12] 1 * 6 + 2 *(-1)+ 3 * 12 = 40 [-1,6,12] 1 *(-1)+ 2 * 6 + 3 * 12 = 47 [-1,12,6] 1 *(-1)+ 2 * 12 + 3 * 6 = 41 [12,-1,6] 1 * 12 + 2 *(-1)+ 3 * 6 = 28 [12,6,-1] 1 * 12 + 2 * 6 + 3 *(-1)= 21所有可能排列的ssc的总和为:27 + 40 + 47 + 41 + 28 + 21 = 204

The maximum value for the ssc is 47. ssc的最大值为47。

The minimum value for the ssc is 21. ssc的最小值是21。

We need a special function ssc_forperm() that receives an array of uncertain number of elements (the elements may occur more than once) and may output a list of dictionaries with the following data: 我们需要一个特殊的函数ssc_forperm(),该函数接收不确定数量的元素的数组(这些元素可能出现多次),并可能输出包含以下数据的字典列表:

[{"total perm":__}, {"total ssc": ___}, {"max ssc": __}, {"min ssc":__}]

My Solution: 我的解决方案:

function permute(input) {
    var permArr = [],
        usedChars = [];
    return (function main() {
        for (var i = 0; i < input.length; i++) {
            var ch = input.splice(i, 1)[0];
            usedChars.push(ch);
            if (input.length === 0) {
                permArr.push(usedChars.slice());
            }
            main();
            input.splice(i, 0, ch);
            usedChars.pop();
        }
        return permArr;
    })();
}

function sscForperm(arr){
  var perm=permute(arr);
  var perm_arr=[];
  var temp_arr=[];
  var j=0;
  while(j<perm.length)
  {
      if(temp_arr.indexOf(perm[j].toString())===-1)
      {
          perm_arr.push(perm[j]);
          temp_arr.push(perm[j].toString());
      }
      j++;
  }
  var total_perm=perm_arr.length;

  var total=0;
  var max= 0;
  var k=0;
  while(k<perm_arr[0].length)
  {
      max+=perm_arr[0][k]*(k+1);
      k++;
  }
  var min=max;
  total+=max;

  var i=1;
  while(i<total_perm)
  {
      var l=0;
      var temp=0;
      while(l<perm_arr[0].length)
      {
          temp+=perm_arr[i][l]*(l+1);
          l++;
      }
      total+=temp;
      if(temp>max)
      {
          max=temp;
      }
      if(temp<min)
      {
          min=temp;
      }
      i++;
  }
  var dict1 = {"total perm":total_perm};
  var dict2 = {"total ssc":total};
  var dict3 = {"max ssc":max};
  var dict4 = {"min ssc":min};
  var ans=[];
  ans.push(dict1);
  ans.push(dict2);
  ans.push(dict3);
  ans.push(dict4);
  return ans;
}

A simple way to optimize your code is using Google Closure Compiler. 优化代码的一种简单方法是使用Google Closure编译器。

Google Closure Compiler Google Closure编译器

I pre-Compiled your code and the code I got is at 我预编译了您的代码,得到的代码在

http://closure-compiler.appspot.com/code/jsc23712967d5b5fa4f457b1638ec432b22/default.js http://closure-compiler.appspot.com/code/jsc23712967d5b5fa4f457b1638ec432b22/default.js

this was fast and easy. 这既快速又容易。 It should now run somewhat faster 现在应该运行得更快

total perm = n!

total ssc can be derived from a formula since each array element appears in the total sum (n - 1)! 因为每个数组元素都出现在总和(n - 1)!中,所以total ssc可以从公式中得出(n - 1)! times for each index: 每个索引的时间:

total ssc = array sum * (n - 1)! * index sum
(6 + 12 - 1) * 2! * 3 * (3 + 1) / 2 = 204

max ssc and min ssc can be computed by simply sorting the array and applying the index multiples in either direction: ascending yields the max, descending the min. 可以通过简单地对数组排序并在任一方向上应用索引倍数来计算max sscmin ssc :升序生成最大值,而降序生成最小值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM