|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | importjava.util.ArrayList;
|
4 |
| -importjava.util.HashMap; |
5 |
| -importjava.util.HashSet; |
6 | 4 | importjava.util.List;
|
7 |
| -importjava.util.Set; |
8 |
| -importjava.util.Map; |
9 | 5 |
|
10 | 6 | publicclass_1557 {
|
11 | 7 | publicstaticclassSolution1 {
|
12 | 8 | publicList<Integer>findSmallestSetOfVertices(intn,List<List<Integer>>edges) {
|
13 |
| -List<Integer>starts =newArrayList<>(); |
14 |
| -Map<Integer,Integer>indegree =newHashMap<>(); |
15 |
| -for (inti =0;i <edges.size();i++) { |
16 |
| -intend =edges.get(i).get(1); |
17 |
| -indegree.put(end,indegree.getOrDefault(end,0) +1); |
| 9 | +int[]indegree =newint[n]; |
| 10 | +for (List<Integer>edge :edges) { |
| 11 | +indegree[edge.get(1)]++; |
18 | 12 | }
|
19 |
| -for (inti =0;i <n;i++) { |
20 |
| -if (!indegree.containsKey(i)) { |
21 |
| -starts.add(i); |
| 13 | +List<Integer>ans =newArrayList<>(); |
| 14 | +for (inti =0;i <indegree.length;i++) { |
| 15 | +if (indegree[i] ==0) { |
| 16 | +ans.add(i); |
22 | 17 | }
|
23 | 18 | }
|
24 |
| -returnstarts; |
| 19 | +returnans; |
25 | 20 | }
|
26 | 21 | }
|
27 | 22 | }
|