|
| 1 | +//Reference - https://leetcode.com/discuss/interview-question/397524/ |
| 2 | + |
| 3 | +/* |
| 4 | +Problem Statement: |
| 5 | +There are several projects, and each is denoted by a one letter name. Each project may depend on one or more other projects (or none). For example, if project A depends on project B, then project A cannot complete before project B. Suppose you are given a list L, of K such dependencies, and also a list D, of J projects that have been delayed. Output a list of all projects that will be delayed, in lexicographical (alphabetical) order. You can assume that a project, A, will be delayed if any project A depends on is delayed. The input is guaranteed to contain no circular dependencies. |
| 6 | +
|
| 7 | +Input: |
| 8 | +
|
| 9 | +Test cases will be provided in the following multiline format. The first line contains one integer, C, which is the number of test cases that will follow. Each test case has the following format. |
| 10 | +
|
| 11 | +The first line of a test case contains two integers, K and J, separated by a space. K is the number of dependencies, and J is the number of delayed projects. K lines follow, each with the format: |
| 12 | +
|
| 13 | +XY |
| 14 | +
|
| 15 | +where X and Y are the names of projects and project X depends on project Y, project names are single uppercase English letters. Each pair gives a project dependency: Y must complete before X can complete. All K lines together form the list L of project dependencies. |
| 16 | +
|
| 17 | +Finally, the last line contains J space-delimited project names (single letters, uppercase). This gives the list D of length J of projects that have been delayed. Each project in D is listed in the dependency list at least once. |
| 18 | +
|
| 19 | +Limits: |
| 20 | +
|
| 21 | +Test case count: 1 <= C <= 20 |
| 22 | +Number of dependencies: 1 <= K <= 100 |
| 23 | +Number of projects: 1 <= J <= 26 |
| 24 | +Project name: Each name is a single uppercase letter from A to Z. |
| 25 | +Outputs: |
| 26 | +For each test case, output one line containing the test case index, starting from 1, followed by a space-delimited list of projects that will be delayed, do not add any space at the end of each line of output. The list must be in lexicographically sorted order. The resulting line should be in this format: |
| 27 | +
|
| 28 | +Case #i: X[1] X[2]... |
| 29 | +
|
| 30 | +where i is the index of the test case, starting from 1, and X[k] are the names of the projects that were delayed. |
| 31 | +
|
| 32 | +Sample Inputs: |
| 33 | +3 |
| 34 | +2 1 |
| 35 | +B A |
| 36 | +C B |
| 37 | +B |
| 38 | +5 2 |
| 39 | +P Q |
| 40 | +P S |
| 41 | +Q R |
| 42 | +R T |
| 43 | +S T |
| 44 | +Q S |
| 45 | +8 2 |
| 46 | +B A |
| 47 | +C B |
| 48 | +C E |
| 49 | +D C |
| 50 | +D F |
| 51 | +E A |
| 52 | +F E |
| 53 | +G F |
| 54 | +B F |
| 55 | +
|
| 56 | +Sample Output: |
| 57 | +Case #1: B C |
| 58 | +Case #2: P Q S |
| 59 | +Case #3: B C D F G |
| 60 | +*/ |
| 61 | + |
| 62 | +// "static void main" must be defined in a public class. |
| 63 | +import java.util.*; |
| 64 | + |
| 65 | +class Solution{ |
| 66 | + //Solution 1 - Using BFS |
| 67 | + public List<Character> findDelayedProjects(char dependencies[][], char delayedProjects[]){ |
| 68 | + List<Character> res = new ArrayList<>(); |
| 69 | + Map<Character, List<Character>> graph = new HashMap<>(); |
| 70 | + Set<Character> visited = new HashSet<>(); |
| 71 | + |
| 72 | + for(char depend[] : dependencies){ |
| 73 | + graph.computeIfAbsent(depend[1], l -> new ArrayList<>()).add(depend[0]); |
| 74 | + } |
| 75 | + |
| 76 | + Queue<Character> q = new LinkedList<>(); |
| 77 | + for(char delay : delayedProjects){ |
| 78 | + q.offer(delay); |
| 79 | + visited.add(delay); |
| 80 | + } |
| 81 | + |
| 82 | + while(!q.isEmpty()){ |
| 83 | + char delayed = q.poll(); |
| 84 | + res.add(delayed); |
| 85 | + List<Character> dependents = graph.get(delayed); |
| 86 | + |
| 87 | + if(dependents != null){ |
| 88 | + for(char d : dependents){ |
| 89 | + if(!visited.contains(d)){ |
| 90 | + q.offer(d); |
| 91 | + visited.add(d); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + Collections.sort(res); |
| 98 | + return res; |
| 99 | + } |
| 100 | + |
| 101 | + //Solution 2 - Using DFS |
| 102 | + public List<Character> findDelayedProjects2(char dependencies[][], char delayedProjects[]){ |
| 103 | + List<Character> res = new ArrayList<>(); |
| 104 | + Map<Character, List<Character>> graph = new HashMap<>(); |
| 105 | + Set<Character> visited = new HashSet<>(); |
| 106 | + |
| 107 | + for(char depend[] : dependencies){ |
| 108 | + graph.computeIfAbsent(depend[1], l -> new ArrayList<>()).add(depend[0]); |
| 109 | + } |
| 110 | + |
| 111 | + for(char delay : delayedProjects){ |
| 112 | + helper(delay, graph, visited, res); |
| 113 | + } |
| 114 | + Collections.sort(res); |
| 115 | + return res; |
| 116 | + } |
| 117 | + |
| 118 | + public void helper(char delay, Map<Character, List<Character>> graph, Set<Character> visited, List<Character> res){ |
| 119 | + if(visited.contains(delay)) return; |
| 120 | + |
| 121 | + res.add(delay); |
| 122 | + visited.add(delay); |
| 123 | + |
| 124 | + List<Character> dependents = graph.get(delay); |
| 125 | + if(dependents != null){ |
| 126 | + for(char dependent : graph.get(delay)){ |
| 127 | + helper(dependent, graph, visited, res); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
| 134 | +public class Main { |
| 135 | + public static void main(String[] args) { |
| 136 | + Solution s = new Solution(); |
| 137 | + Scanner sc = new Scanner(System.in); |
| 138 | + int t = sc.nextInt(); |
| 139 | + int c = 1; |
| 140 | + while(t-- > 0){ |
| 141 | + int projects = sc.nextInt(); |
| 142 | + int delayed = sc.nextInt(); |
| 143 | + |
| 144 | + char dependencies[][] = new char[projects][2]; |
| 145 | + for(int i = 0 ; i < projects ; i++){ |
| 146 | + dependencies[i][0] = sc.next().charAt(0); |
| 147 | + dependencies[i][1] = sc.next().charAt(0); |
| 148 | + } |
| 149 | + |
| 150 | + char delayedProjects[] = new char[delayed]; |
| 151 | + for(int i = 0 ; i < delayed ; i++){ |
| 152 | + delayedProjects[i] = sc.next().charAt(0); |
| 153 | + } |
| 154 | + |
| 155 | + //List<Character> res = s.findDelayedProjects(dependencies, delayedProjects); |
| 156 | + List<Character> res = s.findDelayedProjects2(dependencies, delayedProjects); |
| 157 | + System.out.println("Case #"+c+": "+res); |
| 158 | + c++; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments