Incorrect code got AC: missing Test Case


  • 0
    D

    Incorrect code:

    public class Solution {
        public boolean canJump(int[] A) {
            if(A==null || A.length==0) return false;
            
            int len = A.length;
            boolean[] canJump = new boolean[len];
            canJump[len-1] = true;
            for(int i=len-2; i>=0; i--) {
                //work on index i by looking on elemnts covered after it
                //default is false
                for(int j=Math.min(A[i] + i,len-1); j>=i+1; j--) { //backward is faster
                    if(canJump[j]) canJump[i] = true;
                    break;
                }
            }
            return canJump[0];
        }
    }
    

    Error is here (break should be inside if):
    if(canJump[j]) canJump[i] = true;
    break;

    Test case (2,3,0,1,0 Junit):

    @Test
    public void testSolution2Correctness() {
    	Solution2 sol1 = new Solution2(); 
    	long time = System.currentTimeMillis();
    	
    	System.out.print(sol1.canJump(new int[]{2,3,0,1,0}));
    	System.out.println("  sol 2:   " + (System.currentTimeMillis()-time));
    }

  • 0

    Thanks for pointing this out. A new test case had been added and your code will get Wrong answer now.


Log in to reply
 

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