Find time required to complete the given task

// You are given a set of tasks, dependencies between them and time necessary to complete each of them.
// If one task depends on another, it means it can not start running until the other one is complete.
// There's a machine that runs the tasks. At any given point of time it can work at a single task only.
// You need to write a function that, given one specific task, finds time required to complete this task.
//
// Example:
// Task run times: [1 4 3 5 7]
// Deps: [0,1],[0,2,[2,3,[1,4],

Test case 1:
// Task: 0
// Output: 20

Test case 2:
// Task: 1
// Output: 11

Solution :

public int findTime(int[] executionTime, int[][] deps, int taskId,  int totalTime)
	{
		totalTime += executionTime[taskId];
		for(int[] dep : deps)
		{
			if(dep[0] == taskId)
			{   
				totalTime = processDependentTask(executionTime, deps, dep[1], totalTime);
			} 
		}
		return totalTime;
	}
	
	private int processDependentTask(int[] executionTime, int[][] deps, int dependentId,  int totalTime)
	{
		totalTime += executionTime[dependentId];
		for(int[] dep : deps)
		{
			if(dep[0] == dependentId)
			{   
				dependentId = dep[1];
				totalTime += executionTime[dependentId];
			} 
		}
		return totalTime;
	}
Comments (0)