Keys and Rooms

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.

思路:可以用union find寫,也可以用bfs來做,最後就是判斷是不是一個連通塊;

class Solution {
    
    private class UnionFind {
        private int[] father;
        private int count;
        public UnionFind(int n) {
            this.father = new int[n+1];
            for(int i = 0; i <= n; i++) {
                father[i] = i;
            }
            this.count = n;
        }
        
        public int find(int x) {
            int j = x;
            while(father[j] != j) {
                j = father[j];
            }
            
            // path compression;
            while(j != x) {
                int fx = father[x];
                father[x] = j;
                x = fx;
            }
            return j;
        }
        
        public void union(int a, int b) {
            int root_a = find(a);
            int root_b = find(b);
            if(root_a != root_b) {
                father[root_a] = root_b;
                count--;
            }
        }
        
        public int getCount() {
            return this.count;
        }
    }
    
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        if(rooms == null || rooms.size() == 0) {
            return false;
        }
        int n = rooms.size();
        HashSet<Integer> set = new HashSet<Integer>();
        UnionFind uf = new UnionFind(n);
        dfs(uf, rooms, 0, set);
        return uf.getCount() == 1;
    }
    
    private void dfs(UnionFind uf, List<List<Integer>> rooms, int index, HashSet<Integer> set) {
        List<Integer> list = rooms.get(index);
        for(int i = 0; i < list.size(); i++) {
            uf.union(index, list.get(i));
        }
        set.add(index);
        for(int i = 0; i < list.size(); i++) {
            if(!set.contains(list.get(i))) {
                dfs(uf, rooms, list.get(i), set);
                set.add(list.get(i));
            }
        }
    }
}
class Solution {
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        if(rooms == null || rooms.size() == 0) {
            return false;
        }
        
        Queue<Integer> queue = new LinkedList<Integer>();
        HashSet<Integer> visited = new HashSet<Integer>();
        queue.offer(0);
        visited.add(0);
        
        while(!queue.isEmpty()) {
            Integer index = queue.poll();
            List<Integer> list = rooms.get(index);
            for(int i = 0; i < list.size(); i++) {
                if(!visited.contains(list.get(i))) {
                    visited.add(list.get(i));
                    queue.offer(list.get(i));
                }
            }
        }
        return visited.size() == rooms.size();
    }
}

 

發佈了619 篇原創文章 · 獲贊 13 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章