802. Find Eventual Safe States

In a directed graph, we start at some node and every turn, walk along a directed edge of the graph. If we reach a node that is terminal (that is, it has no outgoing directed edges), we stop.

Now, say our starting node is eventually safe if and only if we must eventually walk to a terminal node. More specifically, there exists a natural number K so that for any choice of where to walk, we must have stopped at a terminal node in less than K steps.

Which nodes are eventually safe? Return them as an array in sorted order.

The directed graph has N nodes with labels 0, 1, ..., N-1, where N is the length of graph. The graph is given in the following form: graph[i] is a list of labels j such that (i, j) is a directed edge of the graph.

Example:
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Here is a diagram of the above graph.
Illustration of graph

Note:

  • graph will have length at most 10000.
  • The number of edges in the graph will not exceed 32000.
  • Each graph[i] will be a sorted list of different integers, chosen within the range [0, graph.length - 1].

一刷:
題解:
其實就是讓判斷一個點是不是出于環(huán)中,輸出所有無環(huán)的路徑上的點。
于是很明顯是dfs,并且可以用dp, 即map來存儲中間結(jié)果

class Solution {
    public List<Integer> eventualSafeNodes(int[][] graph) {
         Map<Integer, Boolean> map = new HashMap<>();
         int len = graph.length;
         
        
        for(int i = 0; i<graph.length; i++){
            boolean[] visited = new boolean[len];
            if(map.containsKey(i)) continue;
            boolean status = true;
            int[] neighbor = graph[i];
            if(neighbor.length == 0) map.put(i, true);
            for(int n : neighbor){
                status = status && dfs(n, map, graph, visited);
            }
            map.put(i, status);
        }
        
        List<Integer> res = new ArrayList<>();
        for (Map.Entry<Integer, Boolean> entry : map.entrySet())
        {
            if(entry.getValue()) res.add(entry.getKey());
        }
        return res;
    }
    
    private boolean dfs(int i, Map<Integer, Boolean> map, int[][] graph, boolean[] visited){
        if(map.containsKey(i)) return map.get(i);
        if(visited[i]){
            map.put(i, false);
            return false;
        }
        visited[i] = true;
        
        int[] neighbor = graph[i];
        if(neighbor.length == 0){
            map.put(i, true);
            return true;
        }
        boolean status = true;
        for(int node : neighbor){
            status = status && dfs(node, map, graph, visited);
        }
        map.put(i, status);
        return status;
    }
}

在discuss看到一個很好的思路,我們可以把map和visited數(shù)組combine, 如果為2,表示有環(huán)(該次visited過),如果為1, 表示之前已經(jīng)檢查過,沒有環(huán)

class Solution {
    public List<Integer> eventualSafeNodes(int[][] graph) {
        List<Integer> res = new ArrayList<>();
        if(graph == null || graph.length == 0) return res;
        
        int nodeCount = graph.length;
        int[] color = new int[nodeCount];
        for(int i=0; i<nodeCount; i++){
            if(dfs(graph, i, color)) res.add(i);
        }
        return res;
    }
    
    private boolean dfs(int[][] graph, int start, int[] color){
        if(color[start] != 0)   return color[start] == 1;//1->true, 2->false
        color[start] = 2;
        for(int neighbor : graph[start]){
            if(!dfs(graph, neighbor, color)) return false;
        }
        color[start] = 1;
        return true;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 今天是女兒四歲的生日。 昨晚,我們躺著講故事聊天。 正準(zhǔn)備道晚安睡覺,他忽然關(guān)了燈和房門。 然后他在黑暗中摸摸索索...
    小牧媽媽閱讀 987評論 0 1
  • (稻盛哲學(xué)學(xué)習(xí)會)打卡第5天 姓名:范學(xué)雷 部門:國際貿(mào)易部 組別:待定 【知~學(xué)習(xí)】 誦讀《活法》將要實現(xiàn)的狀態(tài)...
    James_anjian閱讀 246評論 0 0
  • 不記得在哪里看過一句話:“唯有愛與美食不可辜負(fù)?!蔽揖蜕钌畹孛詰龠@句簡單的話語。總覺得愛與美食之間有一種玄妙的關(guān)系...
    燕燕芭比閱讀 305評論 0 0
  • Thomas S. Monson曾說:圣誕,是不求回報地付出。它是一種幸福,因為我們看到人群的歡愉;是一種自我遺忘...
    小艾侃侃閱讀 154評論 0 0
  • 最近,加了幾個微信好友,都是從qq好友那邊過渡過來的。很麻煩,必須查找qq好友的qq號,再在微信上輸入查找的qq號...
    帥姐賴皮紅閱讀 815評論 9 12

友情鏈接更多精彩內(nèi)容