반응형
https://leetcode.com/problems/find-eventual-safe-states/description/
Find Eventual Safe States - LeetCode
Can you solve this real interview question? Find Eventual Safe States - There is a directed graph of n nodes with each node labeled from 0 to n - 1. The graph is represented by a 0-indexed 2D integer array graph where graph[i] is an integer array of nodes
leetcode.com
각 노드가 terminal node에 도달할 수 있는지 판별해줘야 합니다.
위상정렬을 이용하여 풀 수 있었습니다.
주어진 그래프를 이용하여, 각 노드로 들어오는 간선을 확인합니다.
terminal node에 도달할 수 있는지 판별하려면, terminal node에서부터 시작되는 역방향 간선을 이용하면 됩니다.
초기에 terminal node를 찾아주고, 역방향 연결된 노드들을 위상정렬을 통해 탐색해나가면 됩니다.
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
int n = graph.size();
vector<vector<int>> g(n);
vector<int> ind(n, 0);
queue<int> q;
for(int i=0; i<graph.size(); i++) {
vector<int>& edges = graph[i];
for(int e : edges) {
g[e].push_back(i);
ind[i]++;
}
if(edges.size() == 0) q.push(i);
}
vector<int> res;
while(!q.empty()) {
int node = q.front(); q.pop();
res.push_back(node);
for(int nxt : g[node])
if(--ind[nxt] == 0) q.push(nxt);
}
sort(res.begin(), res.end());
return res;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 445. Add Two Numbers II (0) | 2023.07.17 |
---|---|
LeetCode 1751. Maximum Number of Events That Can Be Attended II (0) | 2023.07.15 |
LeetCode 863. All Nodes Distance K in Binary Tree (0) | 2023.07.11 |
LeetCode 111. Minimum Depth of Binary Tree (0) | 2023.07.10 |
LeetCode 2024. Maximize the Confusion of an Exam (0) | 2023.07.08 |