Leetcode 802. Find Eventual Safe States
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
문제
0번 부터 n-1번까지 숫자가 메겨져 있는 n개의 노드인 그래프가 주어진다. 이 그래프는 0번 인덱스 부터 시작하며 graph는 2차원 배열의 형태를 가지고 있다. graph[i]에는 정수배열이 담가져 있으며 이 정수는 i번 노드에서 갈 수 있는 노드를 의미한다.
이때 아무곳에도 갈 수 없는 노드를 터미널 노드(terminal node)라고 말하며 다른 곳으로 가지 않고 오직 안전 노드(safe node) 또는 터미널 노드에만 갈 수 있는 노드를 안전 노드라고 말한다.
위의 조건을 바탕으로 그래프에 있는 모든 안전노드와 터미널노드의 숫자를 오름차순 정렬하여 반환하라
코드
순환 탐색을 이용해 순환한다면 지나쳐온 모든 노드에 대해 status 값을 조정하여 거르는 형식으로 코드를 작성함