3ms c dfs solution


  • 0
    B
    static bool canCrossInt(int* stones, int stonesSize, int cur, int lastJump) {
        int i, nextJump;
        
        for (i = cur + 1; i < stonesSize; i++) {
            nextJump = stones[i] - stones[cur];
            if (nextJump < lastJump - 1)
                continue;
            if (nextJump > lastJump + 1)
                break;
            if (i == stonesSize - 1 || 
                    canCrossInt(stones, stonesSize, i, nextJump))
                return true;
        }
        return false;
    }
    
    bool canCross(int* stones, int stonesSize) {
        return canCrossInt(stones, stonesSize, 0, 0);
    }
    

Log in to reply
 

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