GithubHelp home page GithubHelp logo

graph-1's Introduction

Graph-1

//Time Complexity = O(N) //Space Complexity = O(N)

class Solution { public int findJudge(int n, int[][] trust) {

    if(trust.length < n-1) {
        return -1;
    }

    int[] in = new int[n+1];
    int[] out = new int[n+1];

    for(int[] t : trust) {
        out[t[0]]++;
        in[t[1]]++;
    }

    for(int i = 1;i <= n; i++) {
        if(in[i] == n-1 && out[i] == 0) {
            return i;
        }
    }
    return -1;
}

}

//Time Complexity = O(MN + (M+N)) //Space Complexity = O(MN)

// class Solution { // boolean result; // int[][] dirs; // int m; // int n; // public boolean hasPath(int[][] maze, int[] start, int[] destination) { // if(maze.length == 0 || maze == null) { // return false; // } // m = maze.length; // n = maze[0].length; // dirs = new int[][] {{1,0},{0,-1},{-1,0},{0,1}};

// return helper(maze,start[0],start[1],destination,m,n); // }

// private boolean helper(int[][] maze, int r,int c, int[] destination,int m,int n) {

// if(r == destination[0] && c == destination[1]) { // return true; // }

// maze[r][c] = 2;

// for(int[] dir : dirs) { // int i = r; // int j = c; // while(i >= 0 && j >= 0 && i < m && j < n && maze[i][j] != 1) { // i += dir[0]; // j += dir[1]; // } // i -= dir[0]; // j -= dir[1];

// if(maze[i][j] != 2 && helper(maze,i, j,destination, m,n)) { // return true; // } // } // return false; // } // }

class Solution { public boolean hasPath(int[][] maze, int[] start, int[] destination) { if(maze.length == 0 || maze[0].length == 0) { return false; } int[][] dirs = new int[][] {{1,0},{0,-1},{-1,0},{0,1}}; Queue queue = new LinkedList<>(); maze[start[0]][start[1]] = 2; queue.add(start[0]); queue.add(start[1]);

    while(!queue.isEmpty()) {
        int r = queue.poll();
        int c = queue.poll();

        for(int[] dir : dirs) {
            int nR = r;
            int nC = c;
            while(nR >= 0 && nC >= 0 && nR<maze.length && nC<maze[0].length && maze[nR][nC] != 1) {
                nR += dir[0];
                nC += dir[1];
            }
            nR -= dir[0];
            nC -= dir[1];

            if(nR == destination[0] && nC == destination[1]) {
                return true;
            }

            if(maze[nR][nC] != 2) {
                queue.add(nR);
                queue.add(nC);
                maze[nR][nC] = 2;
            }
        }
    }
    return false;
}

}

graph-1's People

Contributors

super30admin avatar dasaniaditya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.