3310. Remove Methods From Project
UnknownView on LeetCode
Time: O(n + E)
Space: O(n)
Problem Overview
Remove Methods From Project (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Depth-First Search pattern in coding interviews. DFS from suspicious nodes; mark reachable from invocations; exclude from result.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
DFS from suspicious nodes; mark reachable from invocations; exclude from result.
Related patterns: Array, Depth-First Search, Union Find
3310.cs
C#
// Approach: DFS from suspicious nodes; mark reachable from invocations; exclude from result.
// Time: O(n + E) Space: O(n)
public class Solution
{
private bool[] suspicious;
private bool[] vis;
private List<int>[] f;
private List<int>[] g;
public IList<int> RemainingMethods(int n, int k, int[][] invocations)
{
suspicious = new bool[n];
vis = new bool[n];
f = new List<int>[n];
g = new List<int>[n];
for (int i = 0; i < n; i++)
{
f[i] = new List<int>();
g[i] = new List<int>();
}
foreach (var e in invocations)
{
int a = e[0], b = e[1];
f[a].Add(b);
f[b].Add(a);
g[a].Add(b);
}
Dfs(k);
for (int i = 0; i < n; ++i)
{
if (!suspicious[i] && !vis[i])
Dfs2(i);
}
List<int> ans = new List<int>();
for (int i = 0; i < n; ++i)
{
if (!suspicious[i])
ans.Add(i);
}
return ans;
}
private void Dfs(int i)
{
suspicious[i] = true;
foreach (int j in g[i])
{
if (!suspicious[j])
Dfs(j);
}
}
private void Dfs2(int i)
{
vis[i] = true;
foreach (int j in f[i])
{
if (!vis[j])
{
suspicious[j] = false;
Dfs2(j);
}
}
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)