TLE on leetcode but not on Interviewbit.....


  • 0
    S
    int V = nums.size();
        vector<bool> visited(V,false);
        if(nums.size() <= 1) return true;
            visited[0] = true;
            for(int i = 0;i < nums.size() && visited[i];i++){
                int k = 1;
                while(k <= nums[i] && k+i < nums.size()){
                    visited[k+i] = true;
                    k++;
                }
                if(visited[V-1] == true) return true;
            }
        //visited.clear();
        return false;
    

    even I tried to run TLE testcase as custom testcase which giving me TLE on leetcode while submitting ,but it gives me correct result as custom test case.


  • 0

    @sk_3003
    For OJ, if the problem can be solved in O(n), normally it should be done in O(n) except the implementation of some complicated algorithms like KMP.
    And "submit" and "run" are different, I am not quite sure the mechanism behind but there is one easy explanation is "submit" records the sum of the run times for different test cases, while "run" is just for one test case.
    By the way, this problem can be solved in O(n).

     public boolean canJump(int[] nums) {
        if (nums == null || nums.length == 0) return false;
        int n = nums.length;
        int maxReach = nums[0];
        for (int i = 1; i < n; i++) {
            if ( i > maxReach ) return false;
            maxReach = Math.max(maxReach, i + nums[i]);
        }
        return true;
    }

Log in to reply
 

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