Press "Enter" to skip to content

Posts tagged as “connected components”

花花酱 LeetCode 841. Keys and Rooms

Problem

There areĀ NĀ rooms and you start in roomĀ 0.Ā  Each room has a distinct number inĀ 0, 1, 2, ..., N-1, and each room may haveĀ some keys to access the next room.

Formally, each roomĀ iĀ has a list of keysĀ rooms[i], and each keyĀ rooms[i][j]Ā is an integer inĀ [0, 1, ..., N-1]Ā whereĀ N = rooms.length.Ā  A keyĀ rooms[i][j] = vĀ opens the room with numberĀ v.

Initially, all the rooms start locked (except for roomĀ 0).

You can walk back and forth between rooms freely.

ReturnĀ trueĀ if and only if you can enterĀ every room.

Example 1:

Input: [[1],[2],[3],[]]
Output: true
Explanation:  
We start in room 0, and pick up key 1.
We then go to room 1, and pick up key 2.
We then go to room 2, and pick up key 3.
We then go to room 3.  Since we were able to go to every room, we return true.

Example 2:

Input: [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can't enter the room with number 2.

Note:

  1. 1 <= rooms.length <=Ā 1000
  2. 0 <= rooms[i].length <= 1000
  3. The number of keys in all rooms combined is at mostĀ 3000.

Solution: DFS

Time complexity: O(V + E)

Space complexity: O(V)

C++

 

花花酱 LeetCode 827. Making A Large Island

Problem

In a 2D grid ofĀ 0s andĀ 1s, we change at most oneĀ 0Ā to aĀ 1.

After, what is the size of the largest island?Ā (An island is a 4-directionally connected group ofĀ 1s).

Example 1:

Input: [[1, 0], [0, 1]]
Output: 3
Explanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.

Example 2:

Input: [[1, 1], [1, 0]]
Output: 4
Explanation: Change the 0 to 1 and make the island bigger, only one island with area = 1.

Example 3:

Input: [[1, 1], [1, 1]]
Output: 4
Explanation: Can't change any 0 to 1, only one island with area = 1.

Notes:

  • 1 <= grid.length = grid[0].length <= 50.
  • 0 <= grid[i][j] <= 1.

Solution

Step 1: give each connected component a unique id and count its ara.

Step 2: for each 0 zero, check its 4 neighbours, sum areas up by unique ids.

Time complexity: O(n*m)

Space complexity: O(n*m)

C++

 

花花酱 LeetCode 817. Linked List Components

Problem

题ē›®å¤§ę„ļ¼šē»™ä½ äø€äøŖ链č”Øļ¼Œå†ē»™ä½ äø€äŗ›åˆę³•ēš„节ē‚¹ļ¼Œé—®ä½ é“¾č”Øäø­ęœ‰å¤šå°‘äøŖčæžé€šåˆ†é‡ļ¼ˆę‰€ęœ‰čŠ‚ē‚¹åæ…é”»åˆę³•ļ¼‰ć€‚

https://leetcode.com/problems/linked-list-components/description/

We are givenĀ head,Ā the head node of a linked list containingĀ unique integer values.

We are also given the listĀ G, a subset of the values in the linked list.

Return the number of connected components inĀ G, where two values are connected if they appear consecutively in the linked list.

Example 1:

Input: 
head: 0->1->2->3
G = [0, 1, 3]
Output: 2
Explanation: 
0 and 1 are connected, so [0, 1] and [3] are the two connected components.

Example 2:

Input: 
head: 0->1->2->3->4
G = [0, 3, 1, 4]
Output: 2
Explanation: 
0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.

Note:

  • IfĀ NĀ is theĀ length of the linked list given byĀ head,Ā 1 <= N <= 10000.
  • The value of each node in the linked list will be in the rangeĀ [0, N - 1].
  • 1 <= G.length <= 10000.
  • GĀ is a subset of all values in the linked list.

Solution1: Graph Traversal using DFS

Solution 2: Count tail node in sub graph

 

花花酱 LeetCode 803. Bricks Falling When Hit

Problem

题ē›®å¤§ę„ļ¼šē»™ä½ äø€å µē –墙ļ¼Œę±‚ęÆę¬”å‡»ē¢Žäø€å—åŽęŽ‰č½ēš„ē –夓ꕰ量怂

We have a grid of 1s and 0s; the 1s in a cell represent bricks.Ā  A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.

We will do some erasuresĀ sequentially. Each time we want to do the erasure at the location (i, j), the brick (if it exists) on that location will disappear, and then some other bricks mayĀ drop because of thatĀ erasure.

Return an array representing the number of bricks that will drop after each erasure in sequence.

Example 1:
Input: 
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
Output: [2]
Explanation: 
If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.
Example 2:
Input: 
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
Output: [0,0]
Explanation: 
When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. So each erasure will cause no bricks dropping.  Note that the erased brick (1, 0) will not be counted as a dropped brick.

Idea

  1. For each day, hit and clear the specified brick.
  2. Find all connected components (CCs) using DFS.
  3. For each CC, if there is no brick that is on the first row that the entire cc will drop. Clear those CCs.

Solution: DFS

C++

Java

Related Problems

花花酱 LeetCode 684. Redundant Connection

https://leetcode.com/problems/redundant-connection/description/

Problem:

In this problem, a tree is anĀ undirectedĀ graph that is connected and has no cycles.

The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, …, N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

The resulting graph is given as a 2D-array ofĀ edges. Each element ofĀ edgesĀ is a pairĀ [u, v]Ā withĀ u < v, that represents anĀ undirectedĀ edge connecting nodesĀ uĀ andĀ v.

Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edgeĀ [u, v]Ā should be in the same format, withĀ u < v.

Example 1:

Example 2:

Note:

 

  • The size of the input 2D-array will be between 3 and 1000.
  • Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.



Idea:
DFS / Union-Find

 

Solutions:

C++ / DFS

 

C++ / Union Find

Java / Union Find

 

Python: Union Find

Python / Union Find V2