1ms Java Solution


  • 0
    public boolean isSelfCrossing (int[] x) 
    { 
    
        if (x.length < 4)
            return false;
            
        for (int i = 3 ; i < x.length ; i++) {
            if (isCrossing_2(x, i)) {
                return true;
            }
            
            if (i > 4 && isCrossing_4(x, i))
                return true;
            
            if (i == 4 && x[i-1] == x[i-3] && x[i] >= x[i-2] - x[i-4])
                return true;
        }
        
        return false;
    }
    

    // Check crossing for last 2 movements along same direction

    public boolean isCrossing_2(int[] x, int i) {
        if (x[i-1] <= x[i-3] && x[i] >= x[i-2])
            return true;
            
        return false;
    }
    

    //Check crossing for last 4 movements along same direction

    public boolean isCrossing_4(int[] x, int i) {
        if (x[i-1] <= x[i-3] && x[i-1] >= x[i-3] - x[i-5] && x[i] >=  x[i-2] - x[i-4] && x[i-2] >= x[i-4])
            return true;
            
        return false;
    }

Log in to reply
 

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