Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class Solution {
- public static List<List<String>> create_workflow_stages(List<List<String>> precursor_steps) {
- //
- Map<String, Set<String>> dep = new HashMap<>();
- for (List<String> pair : precursor_steps) {
- String key = pair.get(0);
- Set<String> dependency = dep.get(key);
- if (dependency == null) {
- dependency = new HashSet<>();
- dep.put(key, dependency);
- }
- dependency.add(pair.get(1));
- }
- List<List<String>> ret = new ArrayList<>();
- // Determine keys
- Set<String> visit = new HashSet<>();
- Set<String> data = dep.keySet();
- while(data.equals(visit)) {
- List<String> level = new ArrayList<>();
- for (String s : data) {
- if (visit.contains(s))
- continue;
- if (isSatisfied(visit, dep.get(s))) {
- level.add(s);
- visit.add(s);
- }
- }
- ret.add(level);
- }
- return ret;
- }
- static boolean isSatisfied(Set<String> visit, Set<String> deps) {
- for (String s : deps) {
- if (!visit.contains(s)) {
- return false;
- }
- }
- return true;
- }
- /* START TEST CASES
- * You can add test cases in the two lists below. Each test case
- * input/expected output should correspond to the same index in the
- * respective lists returned.
- */
- static List<List<List<String>>> testInputs = Arrays.asList(
- Arrays.asList(
- Arrays.asList("clean","build"),
- Arrays.asList("metadata","binary"),
- Arrays.asList("build","link"),
- Arrays.asList("link","binary"),
- Arrays.asList("clean","metadata"),
- Arrays.asList("build","resources")
- ),
- Arrays.asList(
- Arrays.asList("boil","serve"),
- Arrays.asList("chop", "boil"),
- Arrays.asList("stir", "boil"),
- Arrays.asList("set table", "serve")
- )
- );
- static List<List<List<String>>> testOutputs = Arrays.asList(
- Arrays.asList(
- Arrays.asList("clean"),
- Arrays.asList("build","metadata"),
- Arrays.asList("resources","link"),
- Arrays.asList("binary")
- ),
- Arrays.asList(
- Arrays.asList("chop","stir","set table"),
- Arrays.asList("boil"),
- Arrays.asList("serve")
- )
- );
- // END TEST CASES
- // DO NOT MODIFY BELOW THIS LINE
- public static boolean equalOutputs(List<List<String>> a, List<List<String>> b) {
- if (a.size() != b.size()) {
- return false;
- }
- for (int i = 0; i < a.size(); i++) {
- List<String> a1 = a.get(i);
- List<String> b1 = b.get(i);
- a1.sort(null);
- b1.sort(null);
- if (!a1.equals(b1)) {
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- for (int i = 0; i < testInputs.size(); i++) {
- try {
- List<List<String>> result = create_workflow_stages(testInputs.get(i));
- if (equalOutputs(result, testOutputs.get(i))) {
- System.out.println("Pass");
- } else {
- System.out.println("Fail");
- }
- } catch (Exception e) {
- System.out.println("Fail");
- System.out.println(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement