Skip to content

Commit 95bae3c

Browse files
add 841
1 parent b7d4b8a commit 95bae3c

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ _If you like this project, please leave me a star._ ★
179179
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | |Easy|
180180
|849|[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_849.java) | |Easy|
181181
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | |Easy|
182+
|841|[Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_841.java) | |Easy|DFS, Graph
182183
|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_840.java) | |Easy|
183184
|836|[Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_836.java) | [:tv:](https://youtu.be/o6hHUk4DOW0) |Easy|
184185
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | |Easy|
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
import java.util.Set;
8+
9+
/**
10+
* 841. Keys and Rooms
11+
*
12+
* There are N rooms and you start in room 0.
13+
* Each room has a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room.
14+
* 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.
15+
* A key rooms[i][j] = v opens the room with number v.
16+
* Initially, all the rooms start locked (except for room 0).
17+
* You can walk back and forth between rooms freely.
18+
* Return true if and only if you can enter every room.
19+
*
20+
* Example 1:
21+
* Input: [[1],[2],[3],[]]
22+
* Output: true
23+
* Explanation:
24+
* We start in room 0, and pick up key 1.
25+
* We then go to room 1, and pick up key 2.
26+
* We then go to room 2, and pick up key 3.
27+
* We then go to room 3. Since we were able to go to every room, we return true.
28+
*
29+
* Example 2:
30+
* Input: [[1,3],[3,0,1],[2],[0]]
31+
* Output: false
32+
* Explanation: We can't enter the room with number 2.
33+
*
34+
* Note:
35+
* 1 <= rooms.length <= 1000
36+
* 0 <= rooms[i].length <= 1000
37+
* The number of keys in all rooms combined is at most 3000.
38+
* */
39+
public class _841 {
40+
public static class Solution1 {
41+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
42+
Set<Integer> unvisitedRooms = new HashSet<>();
43+
for (int i = 1; i < rooms.size(); i++) {
44+
unvisitedRooms.add(i);
45+
}
46+
List<Integer> keys = rooms.get(0);
47+
Queue<Integer> queue = new LinkedList<>();
48+
for (int key : keys) {
49+
queue.offer(key);
50+
}
51+
while (!queue.isEmpty()) {
52+
int size = queue.size();
53+
for (int i = 0; i < size; i++) {
54+
int roomIndex = queue.poll();
55+
unvisitedRooms.remove(roomIndex);
56+
for (int j = 0; j < rooms.get(roomIndex).size(); j++) {
57+
Integer nextRoom = rooms.get(roomIndex).get(j);
58+
if (unvisitedRooms.contains(nextRoom) && !queue.contains(nextRoom)) {
59+
queue.offer(nextRoom);
60+
}
61+
}
62+
}
63+
}
64+
return unvisitedRooms.isEmpty();
65+
}
66+
}
67+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._841;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _841Test {
14+
private static _841.Solution1 solution1;
15+
private static List<List<Integer>> rooms;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _841.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
rooms = new ArrayList<>();
25+
rooms.add(Arrays.asList(1));
26+
rooms.add(Arrays.asList(2));
27+
rooms.add(Arrays.asList(3));
28+
rooms.add(Arrays.asList());
29+
assertEquals(true, solution1.canVisitAllRooms(rooms));
30+
}
31+
32+
@Test
33+
public void test2() {
34+
rooms = new ArrayList<>();
35+
rooms.add(Arrays.asList(1, 3));
36+
rooms.add(Arrays.asList(3, 0, 1));
37+
rooms.add(Arrays.asList(2));
38+
rooms.add(Arrays.asList(0));
39+
assertEquals(false, solution1.canVisitAllRooms(rooms));
40+
}
41+
}

0 commit comments

Comments
 (0)