Jump Game


  • 0
    S

    Hi

    I am always getting time limit exceeded for this question. I have posted the code below. Please suggest me if i am doing anything wrong which is consuming more time

    public boolean canJump(int[] nums)
    	{
    	  int[] result = new int[nums.length];
    	  boolean isJumpPossible = false;
    	  boolean skipCPUCycles = false;
    	  
    	  if(nums.length == 1)
    	   return true;	  
    	 /* result[0] = 0;
    	  for(int a=1;a<result.length;a++)
    	  {
    		result[a] = Integer.MAX_VALUE;  
    	  }*/
    	  for(int i=1;i<nums.length;i++)
    	  {
    		result[i] = Integer.MAX_VALUE;
    		for(int j=0;j<i;j++)
    		{
    		  if((j + nums[j]) >= i && result[j] != Integer.MAX_VALUE)
    		  {
    			int temp = result[j] + 1;
    			if(temp < result[i])
    			 result[i] = temp;	
    			
    			if((j + nums[j]) >= nums.length-1)
    		    {
    			skipCPUCycles = true;
    			isJumpPossible = true;
    			break;  
    		    }
    			
    			if(i == nums.length-1 && result[i] != Integer.MAX_VALUE)
    			{
    			 skipCPUCycles = true;
    			 isJumpPossible = true;
    			 break;
    			}
    		  }
    		}
    		
    		if(skipCPUCycles)
    		 break;	
    	  }
    	  
    	  return isJumpPossible;
    	}
    

    Thanks in advance


Log in to reply
 

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