3310. Remove Methods From Project LeetCode Solution

In this guide, you will get 3310. Remove Methods From Project LeetCode Solution with the best time and space complexity. The solution to Remove Methods From Project problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Remove Methods From Project solution in C++
  4. Remove Methods From Project solution in Java
  5. Remove Methods From Project solution in Python
  6. Additional Resources
3310. Remove Methods From Project LeetCode Solution image

Problem Statement of Remove Methods From Project

You are maintaining a project that has n methods numbered from 0 to n – 1.
You are given two integers n and k, and a 2D integer array invocations, where invocations[i] = [ai, bi] indicates that method ai invokes method bi.
There is a known bug in method k. Method k, along with any method invoked by it, either directly or indirectly, are considered suspicious and we aim to remove them.
A group of methods can only be removed if no method outside the group invokes any methods within it.
Return an array containing all the remaining methods after removing all the suspicious methods. You may return the answer in any order. If it is not possible to remove all the suspicious methods, none should be removed.

Example 1:

Input: n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]
Output: [0,1,2,3]
Explanation:

Method 2 and method 1 are suspicious, but they are directly invoked by methods 3 and 0, which are not suspicious. We return all elements without removing anything.

See also  2300. Successful Pairs of Spells and Potions LeetCode Solution

Example 2:

Input: n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]
Output: [3,4]
Explanation:

Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.

Example 3:

Input: n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]
Output: []
Explanation:

All methods are suspicious. We can remove them.

Constraints:

1 <= n <= 105
0 <= k <= n – 1
0 <= invocations.length <= 2 * 105
invocations[i] == [ai, bi]
0 <= ai, bi <= n – 1
ai != bi
invocations[i] != invocations[j]

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3310. Remove Methods From Project LeetCode Solution in C++

class Solution {
 public:
  vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations) {
    vector<int> ans;
    vector<vector<int>> graph(n);

    for (const vector<int>& invocation : invocations) {
      const int u = invocation[0];
      const int v = invocation[1];
      graph[u].push_back(v);
    }

    queue<int> q{{k}};
    vector<bool> seen(n);
    seen[k] = true;

    while (!q.empty())
      for (int sz = q.size(); sz > 0; --sz) {
        const int u = q.front();
        q.pop();
        for (const int v : graph[u])
          if (!seen[v]) {
            q.push(v);
            seen[v] = true;
          }
      }

    for (int u = 0; u < n; ++u) {
      if (seen[u])
        continue;
      for (const int v : graph[u])
        if (seen[v]) {
          ans.resize(n);
          iota(ans.begin(), ans.end(), 0);
          return ans;
        }
      ans.push_back(u);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3310. Remove Methods From Project LeetCode Solution in Java

class Solution {
  public List<Integer> remainingMethods(int n, int k, int[][] invocations) {
    List<Integer> ans = new ArrayList<>();
    List<Integer>[] graph = new List[n];

    for (int i = 0; i < n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] invocation : invocations) {
      final int u = invocation[0];
      final int v = invocation[1];
      graph[u].add(v);
    }

    Queue<Integer> q = new ArrayDeque<>(List.of(k));
    boolean[] seen = new boolean[n];
    seen[k] = true;

    while (!q.isEmpty())
      for (int sz = q.size(); sz > 0; --sz)
        for (final int v : graph[q.poll()])
          if (!seen[v]) {
            q.offer(v);
            seen[v] = true;
          }

    for (int u = 0; u < n; ++u) {
      if (seen[u])
        continue;
      for (final int v : graph[u])
        if (seen[v]) {
          ans = new ArrayList<>(n);
          for (int i = 0; i < n; ++i)
            ans.add(i);
          return ans;
        }
      ans.add(u);
    }

    return ans;
  }
}
// code provided by PROGIEZ

3310. Remove Methods From Project LeetCode Solution in Python

class Solution:
  def remainingMethods(
      self,
      n: int,
      k: int,
      invocations: list[list[int]]
  ) -> list[int]:
    ans = []
    graph = [[] for _ in range(n)]

    for u, v in invocations:
      graph[u].append(v)

    q = collections.deque([k])
    seen = {k}

    while q:
      for _ in range(len(q)):
        for v in graph[q.popleft()]:
          if v not in seen:
            q.append(v)
            seen.add(v)

    for u in range(n):
      if u in seen:
        continue
      for v in graph[u]:
        if v in seen:
          return list(range(n))
      ans.append(u)

    return ans
# code by PROGIEZ

Additional Resources

See also  962. Maximum Width Ramp LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.