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
Inputn = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]
Output[0,1,2,3]
Methods 1 and 2 are suspicious, but they are directly invoked by methods 0 and 3, which are not suspicious, so nothing can be removed.
Example 2
Inputn = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]
Output[3,4]
Methods 0, 1, and 2 are suspicious and are not directly invoked by any other method, so they can be removed and methods 3 and 4 remain.

Constraints

  • 1 <= n <= 10^5
  • 0 <= k <= n - 1
  • 0 <= invocations.length <= 2 * 10^5
  • invocations[i] == [ai, bi]
  • 0 <= ai, bi <= n - 1
  • ai != bi
  • invocations[i] != invocations[j]

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate