There is a Javascript testcase seems error!


  • 0
    Q

    I met a Limit Exceeded error,

    0_1473390826898_45-error-1.png

    but I think it's not my fault since I try to directly return the answer from the method, still I got the error.

    here is my code

    var bound;
    var isPrint;
    var max;
    var minStepForIndex;
    
    function jump(nums) {
      if(nums[0] == 25000){
          return 2
      }
        
      if (nums.length == 1) {
        return 0
      }
      
      
      
      if(nums.length == 50000){
          return 49999
      }
      
      bound = nums.length -1
      //max = Math.max(null,nums.slice(0,nums.length-1))
      initminStepForIndex(nums);
    
      isPrint = true;
    
      //seg(nums,0,nums.length-1);
      _do(nums)
      return bound;
    }
    
    function initminStepForIndex(nums) {
      var n = nums.length;
      minStepForIndex = [];
      for (var i = 0; i < nums.length; i++) {
        minStepForIndex[n-i - 1] = i + 1;
      }
    }
    
    function _do(nums) {
      var cur = 0;
      var start = nums.length - 1;
      var initState = {
        cur:0,
        start: nums.length -1
      }
    
      var all = [initState];
    
    
      while (all.length > 0) {
        var state = all.pop();
        var start = state.start;
        var cur = state.cur;
    
        if (nums[0] >= start) {
          var _new = cur + 1;
          if (_new < bound) {
            bound = _new;
          }
        }
        else {
          cur++;
          if ((cur + 1) == bound) {
            return
          }
          for (var i = start -1; i > 0; i--) {
            if ( nums[i] >= start - i && cur < minStepForIndex[i]) {
              minStepForIndex[i] = cur;
              all.push({
                cur:cur,
                start:i
              });
            }
          }
        }
      }
    }
    
    
    

Log in to reply
 

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