Why does my JavaScript sulution get a Runtime Error but go well locally?


  • 0
    H

    my code as below, I really wonder a lot..

    var largestDivisibleSubset = function(nums) {
      nums.sort(function(a, b) {
        return a - b;
      });
    
      var ans = [];
    
      var pre = [];
    
      for (var i = 0, len = nums.length; i < len; i++) {
        ans[i] = 1;
        pre[i] = i;
    
        for (var j = 0; j < i; j++) 
          if (nums[i] % nums[j] === 0 && ans[j] + 1 > ans[i]) {
            ans[i] = ans[j] + 1;
            pre[i] = j;
          }
      }
    
    
      var maxn = 0;
      var maxnId;
    
      for (i = 0; i < len; i++) {
        if (ans[i] > maxn) {
          maxn = ans[i];
          maxnId = i;
        }
      }
    
      var ret = [];
    
      while(true) {
        if (maxnId === pre[maxnId]) {
          ret.push(nums[maxnId]);
          break;
        }
    
        ret.push(nums[maxnId]);
        maxnId = pre[maxnId];
      } 
    
      return ret;
    };

  • 0

    Please try submitting again now. I have fixed this issue, now you should get Wrong Answer.


  • 0
    H

    yep, thanks for your quick repairing!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.