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 <= rooms.length <=Ā 1000
0 <= rooms[i].length <= 1000
The number of keys in all rooms combined is at mostĀ 3000.
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].
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
For each day, hit and clear the specified brick.
Find all connected components (CCs) using DFS.
For each CC, if there is no brick that is on the first row that the entire cc will drop. Clear those CCs.
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:
1
2
3
4
5
6
Input:[[1,2],[1,3],[2,3]]
Output:[2,3]
Explanation:The given undirected graph will be like this:
1
/\
2-3
Example 2:
1
2
3
4
5
6
Input:[[1,2],[2,3],[3,4],[1,4],[1,5]]
Output:[1,4]
Explanation:The given undirected graph will be like this:
5-1-2
||
4-3
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.