Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions LeetCode-Problems/802.Find_Eventual_Safe_States/hyorin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Question
[802. Find Eventual Safe States](https://leetcode.com/problems/find-eventual-safe-states/description/)

The goal is to find all the safe nodes, which every path leads to a terminal node.
It means that we want to identify nodes that are not cycle.

# Idea
- Paths starting from safe nodes always end at terminal nodes.
- Therefore, We can start from terminal nodes and work backwards to mark safe nodes.
- This can be using `Topological Sort`

# Topological Sort
Topological Sort is a way to order tasks when some tasks must be done before others.
It only works for grpahs without cycles.

To find nodes not involved in cycles, we perform a reverse topological sort starting from terminal nodes.
Terminal nodes are always safe since they have no outgoing edges (no cycles).

We keep track of each node's outdegree count (number of outgoing edges).
If the count becomes 0, it indicates that node becomes the terminal node, so we remove the node and store it as safe node.

# Code
```swift
class Solution {
func eventualSafeNodes(_ graph: [[Int]]) -> [Int] {
var reversedGraph = Array(repeating: [Int](), count: graph.count)
var outdegrees = Array(repeating: 0, count: graph.count)
var queue = [Int]()

for (index, nodes) in graph.enumerated() {
if nodes.isEmpty {
queue.append(index)
continue
}

outdegrees[index] = nodes.count
for outNode in nodes {
reversedGraph[outNode].append(index)
}
}

var result = [Int]()
while !queue.isEmpty {
let node = queue.removeFirst()
result.append(node)

for inNode in reversedGraph[node] {
outdegrees[inNode] -= 1
if outdegrees[inNode] == 0 {
queue.append(inNode)
}
}
}

return result.sorted()
}
}
```

# Complexity
- Time: O(V + E)
- Space: O(V + E)
V is the number of nodes, E is the number of edges.
39 changes: 39 additions & 0 deletions LeetCode-Problems/997.Find_the_Town_judge/hyorin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Question
[997. Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/description/)

The goal is to find a town judge who is trusted by everyone else.

# Idea
- The town judge trusts nobody.
> The number of people who the town judge trusts is `0`.

- Everybody (except for the town judge) trusts the town judge.
> The number of people who trust the town judge is `number of people - 1`.

# Code
```swift
class Solution {
func findJudge(_ n: Int, _ trust: [[Int]]) -> Int {
var from = Array(repeating: 0, count: n+1)
var to = Array(repeating: 0, count: n+1)

for t in trust {
from[t[0]] += 1
to[t[1]] += 1
}

for index in 1..<n+1 {
if from[index] == 0 && to[index] == n-1 {
return index
}
}

return -1
}
}

```

# Complexity
- Time: O(N+M)
- Space: O(N)