엔지니어 게시판
LeetCode 솔루션 분류

[5/20] 785. Is Graph Bipartite?

컨텐츠 정보

본문

관련자료

댓글 2

mingki님의 댓글

  • 익명
  • 작성일

JayShin님의 댓글

  • 익명
  • 작성일
# Time Complexity: O(n + e), Space Compleixty: O(n)

class Solution:
    def isBipartite(self, graph: List[List[int]]) -> bool:
        n = len(graph)
        colors = [0] * n
        
        def dfs(u, color) -> bool:
            if (colors[u] != 0):
                return colors[u] == color

            colors[u] = color
            for v in graph[u]:
                if (not dfs(v, -color)):
                    return False
            return True
        
        for u in range(n):
            if (colors[u] == 0 and not dfs(u, 1)):
                return False
        return True
전체 396 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 210 명
  • 오늘 방문자 5,189 명
  • 어제 방문자 6,705 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,108 명
알림 0