|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.util.HashMap;
|
5 | 4 | import java.util.HashSet;
|
6 | 5 | import java.util.LinkedList;
|
|
12 | 11 | /**
|
13 | 12 | * 444. Sequence Reconstruction
|
14 | 13 | *
|
15 |
| - * Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence. |
| 14 | + * Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. |
| 15 | + * The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. |
| 16 | + * Reconstruction means building a shortest common supersequence of the sequences in seqs |
| 17 | + * (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). |
| 18 | + * Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence. |
16 | 19 |
|
17 | 20 | Example 1:
|
18 |
| -
|
19 | 21 | Input:
|
20 | 22 | org: [1,2,3], seqs: [[1,2],[1,3]]
|
21 | 23 |
|
|
24 | 26 |
|
25 | 27 | Explanation:
|
26 | 28 | [1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.
|
27 |
| - Example 2: |
28 | 29 |
|
| 30 | + Example 2: |
29 | 31 | Input:
|
30 | 32 | org: [1,2,3], seqs: [[1,2]]
|
31 | 33 |
|
|
34 | 36 |
|
35 | 37 | Explanation:
|
36 | 38 | The reconstructed sequence can only be [1,2].
|
37 |
| - Example 3: |
38 | 39 |
|
| 40 | + Example 3: |
39 | 41 | Input:
|
40 | 42 | org: [1,2,3], seqs: [[1,2],[1,3],[2,3]]
|
41 | 43 |
|
|
44 | 46 |
|
45 | 47 | Explanation:
|
46 | 48 | The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3].
|
47 |
| - Example 4: |
48 | 49 |
|
| 50 | + Example 4: |
49 | 51 | Input:
|
50 | 52 | org: [4,1,5,2,6,3], seqs: [[5,2,6,3],[4,1,5,2]]
|
51 | 53 |
|
52 | 54 | Output:
|
53 | 55 | true
|
54 | 56 | */
|
55 | 57 | public class _444 {
|
56 |
| - |
57 |
| - /**credit: https://discuss.leetcode.com/topic/65948/java-solution-using-bfs-topological-sort*/ |
58 |
| - public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) { |
59 |
| - Map<Integer, Set<Integer>> map = new HashMap<>(); |
60 |
| - Map<Integer, Integer> indegree = new HashMap<>(); |
61 |
| - for (List<Integer> seq : seqs) { |
62 |
| - if (seq.size() == 1) { |
63 |
| - if (!map.containsKey(seq.get(0))) { |
64 |
| - map.put(seq.get(0), new HashSet<>()); |
65 |
| - indegree.put(seq.get(0), 0); |
66 |
| - } |
67 |
| - } else { |
68 |
| - for (int i = 0; i < seq.size() - 1; i++) { |
69 |
| - if (!map.containsKey(seq.get(i))) { |
70 |
| - map.put(seq.get(i), new HashSet<>()); |
71 |
| - indegree.put(seq.get(i), 0); |
72 |
| - } |
73 |
| - |
74 |
| - if (!map.containsKey(seq.get(i + 1))) { |
75 |
| - map.put(seq.get(i + 1), new HashSet<>()); |
76 |
| - indegree.put(seq.get(i + 1), 0); |
| 58 | + public static class Solution1 { |
| 59 | + /** |
| 60 | + * credit: https://discuss.leetcode.com/topic/65948/java-solution-using-bfs-topological-sort |
| 61 | + */ |
| 62 | + public boolean sequenceReconstruction(int[] org, List<List<Integer>> seqs) { |
| 63 | + Map<Integer, Set<Integer>> map = new HashMap<>(); |
| 64 | + Map<Integer, Integer> indegree = new HashMap<>(); |
| 65 | + for (List<Integer> seq : seqs) { |
| 66 | + if (seq.size() == 1) { |
| 67 | + if (!map.containsKey(seq.get(0))) { |
| 68 | + map.put(seq.get(0), new HashSet<>()); |
| 69 | + indegree.put(seq.get(0), 0); |
77 | 70 | }
|
78 |
| - |
79 |
| - if (map.get(seq.get(i)).add(seq.get(i + 1))) { |
80 |
| - indegree.put(seq.get(i + 1), indegree.get(seq.get(i + 1)) + 1); |
| 71 | + } else { |
| 72 | + for (int i = 0; i < seq.size() - 1; i++) { |
| 73 | + if (!map.containsKey(seq.get(i))) { |
| 74 | + map.put(seq.get(i), new HashSet<>()); |
| 75 | + indegree.put(seq.get(i), 0); |
| 76 | + } |
| 77 | + |
| 78 | + if (!map.containsKey(seq.get(i + 1))) { |
| 79 | + map.put(seq.get(i + 1), new HashSet<>()); |
| 80 | + indegree.put(seq.get(i + 1), 0); |
| 81 | + } |
| 82 | + |
| 83 | + if (map.get(seq.get(i)).add(seq.get(i + 1))) { |
| 84 | + indegree.put(seq.get(i + 1), indegree.get(seq.get(i + 1)) + 1); |
| 85 | + } |
81 | 86 | }
|
82 | 87 | }
|
83 | 88 | }
|
84 |
| - } |
85 | 89 |
|
86 |
| - Queue<Integer> queue = new LinkedList<>(); |
87 |
| - for (Integer key : indegree.keySet()) { |
88 |
| - if (indegree.get(key) == 0) { |
89 |
| - queue.offer(key); |
| 90 | + Queue<Integer> queue = new LinkedList<>(); |
| 91 | + for (Integer key : indegree.keySet()) { |
| 92 | + if (indegree.get(key) == 0) { |
| 93 | + queue.offer(key); |
| 94 | + } |
90 | 95 | }
|
91 |
| - } |
92 | 96 |
|
93 |
| - int index = 0; |
94 |
| - while (!queue.isEmpty()) { |
95 |
| - int size = queue.size(); |
96 |
| - if (size > 1) { |
97 |
| - return false; |
98 |
| - } |
99 |
| - int curr = queue.poll(); |
100 |
| - if (index == org.length || curr != org[index++]) { |
101 |
| - return false; |
102 |
| - } |
103 |
| - for (int next : map.get(curr)) { |
104 |
| - indegree.put(next, indegree.get(next) - 1); |
105 |
| - if (indegree.get(next) == 0) { |
106 |
| - queue.offer(next); |
| 97 | + int index = 0; |
| 98 | + while (!queue.isEmpty()) { |
| 99 | + int size = queue.size(); |
| 100 | + if (size > 1) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + int curr = queue.poll(); |
| 104 | + if (index == org.length || curr != org[index++]) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + for (int next : map.get(curr)) { |
| 108 | + indegree.put(next, indegree.get(next) - 1); |
| 109 | + if (indegree.get(next) == 0) { |
| 110 | + queue.offer(next); |
| 111 | + } |
107 | 112 | }
|
108 | 113 | }
|
| 114 | + return index == org.length && index == map.size(); |
109 | 115 | }
|
110 |
| - return index == org.length && index == map.size(); |
111 | 116 | }
|
112 |
| - |
113 | 117 | }
|
0 commit comments