Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Solution {
  5. public static List<List<String>> create_workflow_stages(List<List<String>> precursor_steps) {
  6. //
  7. Map<String, Set<String>> dep = new HashMap<>();
  8.  
  9. for (List<String> pair : precursor_steps) {
  10. String key = pair.get(0);
  11.  
  12. Set<String> dependency = dep.get(key);
  13.  
  14. if (dependency == null) {
  15. dependency = new HashSet<>();
  16. dep.put(key, dependency);
  17. }
  18.  
  19. dependency.add(pair.get(1));
  20.  
  21. }
  22.  
  23. List<List<String>> ret = new ArrayList<>();
  24.  
  25. // Determine keys
  26. Set<String> visit = new HashSet<>();
  27. Set<String> data = dep.keySet();
  28. while(data.equals(visit)) {
  29. List<String> level = new ArrayList<>();
  30.  
  31. for (String s : data) {
  32. if (visit.contains(s))
  33. continue;
  34.  
  35.  
  36. if (isSatisfied(visit, dep.get(s))) {
  37. level.add(s);
  38. visit.add(s);
  39. }
  40. }
  41.  
  42. ret.add(level);
  43. }
  44.  
  45. return ret;
  46. }
  47.  
  48. static boolean isSatisfied(Set<String> visit, Set<String> deps) {
  49. for (String s : deps) {
  50. if (!visit.contains(s)) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56.  
  57.  
  58.  
  59. /* START TEST CASES
  60. * You can add test cases in the two lists below. Each test case
  61. * input/expected output should correspond to the same index in the
  62. * respective lists returned.
  63. */
  64. static List<List<List<String>>> testInputs = Arrays.asList(
  65. Arrays.asList(
  66. Arrays.asList("clean","build"),
  67. Arrays.asList("metadata","binary"),
  68. Arrays.asList("build","link"),
  69. Arrays.asList("link","binary"),
  70. Arrays.asList("clean","metadata"),
  71. Arrays.asList("build","resources")
  72. ),
  73. Arrays.asList(
  74. Arrays.asList("boil","serve"),
  75. Arrays.asList("chop", "boil"),
  76. Arrays.asList("stir", "boil"),
  77. Arrays.asList("set table", "serve")
  78. )
  79. );
  80.  
  81. static List<List<List<String>>> testOutputs = Arrays.asList(
  82. Arrays.asList(
  83. Arrays.asList("clean"),
  84. Arrays.asList("build","metadata"),
  85. Arrays.asList("resources","link"),
  86. Arrays.asList("binary")
  87. ),
  88. Arrays.asList(
  89. Arrays.asList("chop","stir","set table"),
  90. Arrays.asList("boil"),
  91. Arrays.asList("serve")
  92. )
  93. );
  94. // END TEST CASES
  95.  
  96. // DO NOT MODIFY BELOW THIS LINE
  97. public static boolean equalOutputs(List<List<String>> a, List<List<String>> b) {
  98. if (a.size() != b.size()) {
  99. return false;
  100. }
  101. for (int i = 0; i < a.size(); i++) {
  102. List<String> a1 = a.get(i);
  103. List<String> b1 = b.get(i);
  104. a1.sort(null);
  105. b1.sort(null);
  106. if (!a1.equals(b1)) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112.  
  113. public static void main(String[] args) {
  114. for (int i = 0; i < testInputs.size(); i++) {
  115. try {
  116. List<List<String>> result = create_workflow_stages(testInputs.get(i));
  117.  
  118. if (equalOutputs(result, testOutputs.get(i))) {
  119. System.out.println("Pass");
  120. } else {
  121. System.out.println("Fail");
  122. }
  123. } catch (Exception e) {
  124. System.out.println("Fail");
  125. System.out.println(e);
  126. }
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement