Problem · Graph

Task Master (Also for Infra Automation Intern)

Learn this problem
MediumSnowflake logoSnowflakeINTERNOA
See Snowflake hiring insights

Problem statement

Eric is the most methodical employee at the Acme company. His manager assigned him a number of tasks for the quarter, and gave him a list of notes regarding the order they must be performed. Each note states that some task must be completed before some related task. If he goes to perform some task and sees that a rule exists requiring that this task be performed before an already completed task, then he cannot perform the task. Help Eric determine the maximum number of tasks he can complete.

Function

tasks(n: int, a: int[], b: int[]) → int

Complete the function tasks in the editor. It must return an integer denoting the maximum number of tasks that Eric can complete.

tasks has the following parameters(s):

  • int n: integer, number of tasks
  • a[a[0],...a[n-1]]: an array of integers, the dependent tasks
  • b[b[0],...b[n-1]]: an array of integers, the primary tasks
  • Examples

    Example 1

    n = 7a = [1, 2, 3, 4, 6, 5]b = [7, 6, 4, 1, 2, 1]return = 6
    Example 1 illustration
    For example, Eric has n=7 tasks to complete. His manager gives him m=6 notes on the order tasks must be performed. Here is a graph of the dependencies. The dependent array, a = [1, 2, 3, 4, 6, 5]. His principal tasks array, b = [7, 6, 4, 1, 2, 1]. Here is a graph of the dependencies: From the graph, it is easy to see that task 6 must be performed before task 2 and vice versa. He can only complete one of those two tasks before the other, so he must choose either task 6 or 2. He can complete 7 - 1 = 6 tasks.

    Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ m ≤ n
  • Each task depends on at most one other task.
  • More Snowflake problems

    drafts saved locally
    public int tasks(int n, int[] a, int[] b) {
      // write your code here
    }
    
    n7
    a[1, 2, 3, 4, 6, 5]
    b[7, 6, 4, 1, 2, 1]
    expected6
    checking account