Skip to content

Instantly share code, notes, and snippets.

@sarthakpranesh
Created August 23, 2020 14:09
Show Gist options
  • Save sarthakpranesh/7ce0beaea96239ad86a94c8c2e7f6018 to your computer and use it in GitHub Desktop.
Save sarthakpranesh/7ce0beaea96239ad86a94c8c2e7f6018 to your computer and use it in GitHub Desktop.
class Solution {
public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) {
List<Integer> l = new ArrayList<>();
List<Integer> no = new ArrayList<>();
for (int i = 0; i < edges.size(); ++i) {
no.add(edges.get(i).get(1));
int isThereInL = l.indexOf(edges.get(i).get(1));
if (isThereInL != -1) {
l.remove(isThereInL);
}
int curEdge = edges.get(i).get(0);
if (l.indexOf(curEdge) != -1 || no.indexOf(curEdge) != -1) {
continue;
}
l.add(curEdge);
}
return l;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment