Is Graph Bipartite?
MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.You get an undirected graph as an adjacency list: graph[u] lists the neighbours of node u. The graph may not be connected. Return true if you can split the nodes into two groups so that every edge joins nodes from different groups, and false otherwise.
graph = [[1, 3], [0, 2], [1, 3], [0, 2]] → true (groups {0, 2} and {1, 3})
graph = [[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]] → false (0, 1, 2 form a triangle)Constraints
1 <= graph.length <= 1000 <= graph[u].length < graph.length- No self-loops, no repeated neighbours, and if
vis ingraph[u]thenuis ingraph[v]