Skip to content

Commit 3896394

Browse files
committed
update 444 java
1 parent ed94064 commit 3896394

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

leetcode_java/src/main/java/LeetCodeJava/BFS/SequenceReconstruction.java

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,103 @@
6060
public class SequenceReconstruction {
6161

6262
// V0
63-
// TODO : implement
63+
// TODO : validate below
64+
// IDEA : TOPOLOGICAL SORT. LC 210
6465
// public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences) {
6566
//
67+
// if (sequences.size()==0){
68+
// return true;
69+
// }
70+
//
71+
// return TopologicalSort(nums, sequences);
72+
// }
73+
//
74+
// public boolean TopologicalSort(int[] nums, List<List<Integer>> edges) {
75+
// // Step 1: Build the graph and calculate in-degrees
76+
// Map<Integer, List<Integer>> graph = new HashMap<>();
77+
// int[] inDegree = new int[nums.length];
78+
//
79+
// for (int i = 0; i < nums.length; i++) {
80+
// graph.put(i, new ArrayList<>());
81+
// }
82+
//
83+
// for (List<Integer> edge : edges) {
84+
// int from = edge.get(0);
85+
// int to = edge.get(1);
86+
// graph.get(from).add(to);
87+
// inDegree[to]++;
88+
// }
89+
//
90+
// // Step 2: Initialize a queue with nodes that have in-degree 0
91+
// Queue<Integer> queue = new LinkedList<>();
92+
// for (int i = 0; i < nums.length; i++) {
93+
// /**
94+
// * NOTE !!!
95+
// *
96+
// * we add ALL nodes with degree = 0 to queue at init step
97+
// */
98+
// if (inDegree[i] == 0) {
99+
// queue.offer(i);
100+
// }
101+
// }
102+
//
103+
// List<Integer> topologicalOrder = new ArrayList<>();
104+
//
105+
// // Step 3: Process the nodes in topological order
106+
// while (!queue.isEmpty()) {
107+
//
108+
// /**
109+
// * NOTE !!!
110+
// *
111+
// * if queue size > 1,
112+
// * means there are MORE THAN 1 possible solution
113+
// * -> return false directly
114+
// *
115+
// */
116+
// if (queue.size() != 1){
117+
// return false;
118+
// }
119+
//
120+
// /**
121+
// * NOTE !!!
122+
// *
123+
// * ONLY "degree = 0" nodes CAN be added to queue
124+
// *
125+
// * -> so we can add whatever node from queue to final result (topologicalOrder)
126+
// */
127+
// int current = queue.poll();
128+
//
129+
//
130+
// // TODO : fix below
131+
// //if(curr != nums[index++]) return false;
132+
//
133+
// topologicalOrder.add(current);
134+
//
135+
// for (int neighbor : graph.get(current)) {
136+
// inDegree[neighbor] -= 1;
137+
// /**
138+
// * NOTE !!!
139+
// *
140+
// * if a node "degree = 0" means this node can be ACCESSED now,
141+
// *
142+
// * -> so we need to add it to the queue (for adding to topologicalOrder in the following while loop iteration)
143+
// */
144+
// if (inDegree[neighbor] == 0) {
145+
// queue.offer(neighbor);
146+
// }
147+
// }
148+
// }
149+
//
150+
// // If topologicalOrder does not contain all nodes, there was a cycle in the graph
151+
// if (topologicalOrder.size() != nums.length) {
152+
// //throw new IllegalArgumentException("The graph has a cycle, so topological sort is not possible.");
153+
// return false;
154+
// }
155+
//
156+
// /** NOTE !!! reverse ordering */
157+
// //Collections.reverse(topologicalOrder);
158+
// //return topologicalOrder;
159+
// return true;
66160
// }
67161

68162
// V1

0 commit comments

Comments
 (0)