TheAlgorithms-C/leetcode/src/841.c
Alexander Pantyukhin 103361de54
feat: add Keys and Rooms LeetCode problem (#1189)
Co-authored-by: David Leal <halfpacho@gmail.com>
2023-01-19 22:09:32 -06:00

28 lines
792 B
C

void visitRooms(int key, int** rooms, int roomsSize, int* roomsColSize, int* visitedRooms){
if (visitedRooms[key] == 1){
return;
}
visitedRooms[key] = 1;
for (int i = 0; i < roomsColSize[key]; i++){
visitRooms(rooms[key][i], rooms, roomsSize, roomsColSize, visitedRooms);
}
}
// Depth-first search
// Runtime: O(n)
// Space: O(n)
bool canVisitAllRooms(int** rooms, int roomsSize, int* roomsColSize){
int* visitedRooms = calloc(roomsSize, sizeof(int));
visitRooms(0, rooms, roomsSize, roomsColSize, visitedRooms);
int visitedRoomsNumber = 0;
for (int i = 0; i < roomsSize; i++){
if (visitedRooms[i] == 1){
visitedRoomsNumber++;
}
}
return visitedRoomsNumber == roomsSize;
}