Can anyone tell me why Time Limit Exceeded for my code?


  • 0
    C

    public class Solution extends VersionControl {

    public int firstBadVersion(int n) {
    
        int i = 1;
        int j = n;
        
        while(i < j){
            int mid = (i + j) /2;
            
            if(!isBadVersion(mid)){
                i = mid + 1;
            }else{
                j = mid;
            }
        }
        
        return i;
    }
    

    }


  • 0
    C

    Should be int mid = i + (j - i) /2;


Log in to reply
 

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