import java.util.*;
class Solution {
public static boolean[] unlocked;
public static List<List<Integer>> room;
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
int size = rooms.size();
room = rooms;
unlocked = new boolean[size];
unlock(0);
boolean result = true;
for(boolean check: unlocked) {
if (!check) {
result = false;
break;
}
}
return result;
}
public void unlock(int visitedRoom) {
if (unlocked[visitedRoom]) {
return;
}
unlocked[visitedRoom] = true;
List<Integer> keys = room.get(visitedRoom);
for(int key: keys) {
unlock(key);
}
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 215. Kth Largest Element in an Array (0) | 2024.05.28 |
---|---|
[leetcode] 1926. Nearest Exit from Entrance in Maze (0) | 2024.05.26 |
[leetcode] 104. Maximum Depth of Binary Tree (0) | 2024.05.23 |
[leetcode] 1732. Find the Highest Altitude (0) | 2024.05.18 |
[leetcode] 643. Maximum Average Subarray I (0) | 2024.05.18 |