Skip to content

Commit 69cf313

Browse files
refactor 444
1 parent 381a444 commit 69cf313

File tree

2 files changed

+59
-58
lines changed

2 files changed

+59
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.HashMap;
54
import java.util.HashSet;
65
import java.util.LinkedList;
@@ -12,10 +11,13 @@
1211
/**
1312
* 444. Sequence Reconstruction
1413
*
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.
1619
1720
Example 1:
18-
1921
Input:
2022
org: [1,2,3], seqs: [[1,2],[1,3]]
2123
@@ -24,8 +26,8 @@
2426
2527
Explanation:
2628
[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:
2829
30+
Example 2:
2931
Input:
3032
org: [1,2,3], seqs: [[1,2]]
3133
@@ -34,8 +36,8 @@
3436
3537
Explanation:
3638
The reconstructed sequence can only be [1,2].
37-
Example 3:
3839
40+
Example 3:
3941
Input:
4042
org: [1,2,3], seqs: [[1,2],[1,3],[2,3]]
4143
@@ -44,70 +46,72 @@
4446
4547
Explanation:
4648
The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3].
47-
Example 4:
4849
50+
Example 4:
4951
Input:
5052
org: [4,1,5,2,6,3], seqs: [[5,2,6,3],[4,1,5,2]]
5153
5254
Output:
5355
true
5456
*/
5557
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);
7770
}
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+
}
8186
}
8287
}
8388
}
84-
}
8589

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+
}
9095
}
91-
}
9296

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+
}
107112
}
108113
}
114+
return index == org.length && index == map.size();
109115
}
110-
return index == org.length && index == map.size();
111116
}
112-
113117
}

src/test/java/com/fishercoder/_444Test.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010

1111
import static org.junit.Assert.assertEquals;
1212

13-
/**
14-
* Created by stevesun on 6/3/17.
15-
*/
1613
public class _444Test {
17-
private static _444 test;
14+
private static _444.Solution1 solution1;
1815
private static int[] org;
1916
private static List<List<Integer>> seqs;
2017

2118
@BeforeClass
2219
public static void setup() {
23-
test = new _444();
20+
solution1 = new _444.Solution1();
2421
}
2522

2623
@Test
@@ -29,6 +26,6 @@ public void test1() {
2926
seqs = new ArrayList<>();
3027
seqs.add(Arrays.asList(1, 2));
3128
seqs.add(Arrays.asList(1, 3));
32-
assertEquals(false, test.sequenceReconstruction(org, seqs));
29+
assertEquals(false, solution1.sequenceReconstruction(org, seqs));
3330
}
3431
}

0 commit comments

Comments
 (0)