Skip to content

Latest commit

 

History

History
206 lines (163 loc) · 11.2 KB

File metadata and controls

206 lines (163 loc) · 11.2 KB

6 GRAPH PROBLEMS SOLVED | LeetCode Grind 2023 | Blind 75 List

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to Graph Problems in Blind 75

The video covers six graph problems from the Blind 75 LeetCode list, focusing on common patterns like DFS, graph cloning, and cycle detection. It uses Python implementations and explains time/space complexities.

Key Takeaway: These problems build on graph traversal techniques, emphasizing visited sets to avoid cycles and efficient exploration.

Ask AI: Blind 75 Graph Problems

Number of Islands

You're given a 2D grid where '1's are land and '0's are water. Count the number of islands (groups of connected '1's surrounded by '0's).

Summary: Use DFS to explore each unvisited land cell, marking visited cells to avoid revisits. Increment the island count for each new land cell found, and explore in all four directions.

Key Takeaway/Example: Time and space complexity is O(rows * cols) due to visiting each cell once. Here's a simplified DFS snippet:

def dfs(grid, r, c, visited):
    if not (0 <= r < len(grid) and 0 <= c < len(grid[0]) and (r, c) not in visited and grid[r][c] == '1'):
        return
    visited.add((r, c))
    dfs(grid, r-1, c, visited)  # up
    dfs(grid, r+1, c, visited)  # down
    dfs(grid, r, c+1, visited)  # right
    dfs(grid, r, c-1, visited)  # left

Iterate through the grid and call DFS on unvisited '1's, incrementing the count.

Ask AI: Number of Islands

Clone Graph

Given a graph, create a deep clone with new nodes and identical connections.

Summary: Use DFS iteratively with a stack and a hash map to map old nodes to new ones. Build new nodes during traversal, then connect their neighbors using the map.

Key Takeaway/Example: Handles cycles with a visited set. Time/space O(N + E) for nodes and edges. Example mapping:

old_to_new = {}
stack = [node]
visited = set()
while stack:
    old = stack.pop()
    if old in visited: continue
    visited.add(old)
    old_to_new[old] = Node(old.val)
    for neigh in old.neighbors:
        stack.append(neigh)
# Then connect neighbors
for old, new in old_to_new.items():
    for neigh in old.neighbors:
        new.neighbors.append(old_to_new[neigh])

Return the new node for the original head.

Ask AI: Clone Graph

Pacific Atlantic Water Flow

In a 2D grid of heights, find cells where water can flow to both Pacific (top/left) and Atlantic (bottom/right) oceans. Water flows to equal or lower heights.

Summary: Perform DFS from Pacific borders inward, marking reachable cells. Repeat from Atlantic borders. The intersection of both sets are the valid cells.

Key Takeaway/Example: Reverse the thinking: find what oceans can reach instead of what cells can reach oceans. DFS checks bounds, visited, and height constraints (current >= previous).

def dfs(grid, r, c, visited, prev):
    if not (0 <= r < len(grid) and 0 <= c < len(grid[0]) and (r, c) not in visited and grid[r][c] >= prev):
        return
    visited.add((r, c))
    dfs(grid, r-1, c, visited, grid[r][c])
    dfs(grid, r+1, c, visited, grid[r][c])
    dfs(grid, r, c+1, visited, grid[r][c])
    dfs(grid, r, c-1, visited, grid[r][c])

Call DFS from borders, collect intersection.

Ask AI: Pacific Atlantic Water Flow

Course Schedule

Given courses and prerequisites, determine if you can finish all courses (no cycles in the graph).

Summary: Build a graph of prerequisites. Use DFS to detect cycles by tracking the current path and resolved courses. If a prerequisite is in the current path, there's a cycle.

Key Takeaway/Example: Returns false on cycle. Uses sets for path and resolved.

def dfs(course, graph, path, resolved):
    if course in resolved: return True
    if course in path: return False
    path.add(course)
    for prereq in graph[course]:
        if not dfs(prereq, graph, path, resolved):
            return False
    path.remove(course)
    resolved.add(course)
    return True

Iterate through all courses, return false if any DFS fails.

Ask AI: Course Schedule

Number of Connected Components

Given an undirected graph, count the connected components.

Summary: Use Union-Find to merge components via edges. Count roots (where parent is self) for the number of components.

Key Takeaway/Example: Union-Find with path compression and union by rank. Time O(N + E * α(N)).

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [0] * n
    
    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    
    def union(self, x, y):
        rx, ry = self.find(x), self.find(y)
        if rx == ry: return
        if self.rank[rx] > self.rank[ry]:
            self.parent[ry] = rx
        elif self.rank[rx] < self.rank[ry]:
            self.parent[rx] = ry
        else:
            self.parent[ry] = rx
            self.rank[rx] += 1

Union all edges, count nodes where find(i) == i.

Ask AI: Number of Connected Components

Graph Valid Tree

Given a graph, check if it's a valid tree (connected, no cycles).

Summary: Reuse Union-Find from previous problem. Union edges; if roots are same before union, cycle exists. Finally, check for exactly one component (one root).

Key Takeaway/Example: Detects cycles during union. Returns false if cycle or multiple components.

# Using same UnionFind class
uf = UnionFind(n)
for x, y in edges:
    if uf.find(x) == uf.find(y):
        return False
    uf.union(x, y)
# Count roots
roots = sum(uf.find(i) == i for i in range(n))
return roots == 1

Ask AI: Graph Valid Tree

Wrapping Up the Series

The video mentions upcoming dynamic programming problems and references previous videos on trees, linked lists, etc.

Summary: Encourages subscribing for more LeetCode content.

Ask AI: LeetCode Blind 75 Series


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: