/**
* The Problem:
*
* We have a list of tasks. Each task can depend on other tasks.
* For example if task A depends on task B then B should run before A.
*
* Implement the "getTaskWithDependencies" method such that it returns a list of task names in the correct order.
*
* For example if I want to execute task "application A", the method should return a list with "storage,mongo,application A".
*
* List of Tasks:
*
* - name: application A
* dependsOn:
* - mongo
*
* - name: storage
*
* - name: mongo
* dependsOn:
* - storage
*
* - name: memcache
*
* - name: application B
* dependsOn:
* - memcache
*
* The Java program is expected to be executed succesfully.
*/
public class Solution {
private static List<String> getTaskWithDependencies(List<Task> tasks, String dependsOn) {
// TODO: please implement logic here
}
@Test
public void testGetTaskDependenciesForApplicationA() {
Assert.assertEquals(
Arrays.asList(
"storage",
"mongo",
"application A"
),
getTaskWithDependencies(TaskList.getTasks(), "application A")
);
}
}
/**
* Definition of a Task
*/
class Task {
private final String name;
private final List<String> dependsOn;
Task(String name) {
this(name, new ArrayList<>());
}
Task(String name, List<String> dependsOn) {
this.name = name;
this.dependsOn = dependsOn;
}
public String getName() { return this.name; }
public List<String> getDependsOn() { return this.dependsOn; }
}
class TaskList {
public static List<Task> getTasks() {
List<Task> tasks = new ArrayList<>();
tasks.add(new Task("application A", Arrays.asList("mongo")));
tasks.add(new Task("storage"));
tasks.add(new Task("mongo", Arrays.asList("storage")));
tasks.add(new Task("memcache"));
tasks.add(new Task("application B", Arrays.asList("memcache")));
return tasks;
}
}
The general idea is to give some directional relationships, including: application a -> mongodb -> storage This is a connecting path application b -> memcache This is a connecting path, Now that application A is specified, it is required to output the connected path where it is. My approach is to sort these 5 points together using topological sorting. The result will be b->memcache->a->mongo->storage. My question is: how to output only one connected path and ignore the other? I read there is no similar question in Leetcode, so there is no idea. I can write the code by myself, and it’s good to be able to tell me the idea.