LeetCode Keys and Rooms Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

why dont use hashset? if hashset size < n, false.

mikexue
Автор

BFS Solution:
class Solution {
public boolean rooms) {
Set<Integer> visited = new HashSet<>();

Queue<Integer> queue = new LinkedList<>();
queue.add(0);
while(!queue.isEmpty()) {
int room = queue.poll();
if(!visited.contains(room)) {
visited.add(room);
List<Integer> keys = rooms.get(room);
queue.addAll(keys);
}
}

return visited.size() == rooms.size();
}
}

KiranVuyyuru
Автор

When i think you cant have worst explanations..there you are.

alxx
Автор

I am wondering how keys.add worked with stack....it's .push with stack

rupaldesai