Problem solution · Java

Find All People With Secret

Find All People With Secret: a Java solution using disjoint set union. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Disjoint set union
Source
walkccc LeetCode Solutions
Length
77 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Disjoint set union

For Find All People With Secret, the implementation maintains connected components and merges them as relationships are processed.

  1. Give each element a component representative.
  2. Merge representatives when a connection is accepted.
  3. Answer connectivity or component queries from the compressed representatives.

Code notes

  • 77 lines of Java from the credited upstream file 2092.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • 6 loop blocks detected.

Complexity

Account for every find and union operation; with path compression and ranked merging, the amortized cost is nearly constant per operation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind All People With Secret · JavaJava
Use this to learn the idea, then write your own version.
class UnionFind {  public UnionFind(int n) {    id = new int[n];    rank = new int[n];    for (int i = 0; i < n; ++i)      id[i] = i;  }   public void unionByRank(int u, int v) {    final int i = find(u);    final int j = find(v);    if (i == j)      return;    if (rank[i] < rank[j]) {      id[i] = j;    } else if (rank[i] > rank[j]) {      id[j] = i;    } else {      id[i] = j;      ++rank[j];    }  }   public boolean connected(int u, int v) {    return find(u) == find(v);  }   public void reset(int u) {    id[u] = u;  }   private int[] id;  private int[] rank;   private int find(int u) {    return id[u] == u ? u : (id[u] = find(id[u]));  }} class Solution {  public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {    List<Integer> ans = new ArrayList<>();    UnionFind uf = new UnionFind(n);    TreeMap<Integer, List<Pair<Integer, Integer>>> timeToPairs = new TreeMap<>();     uf.unionByRank(0, firstPerson);     for (int[] meeting : meetings) {      final int x = meeting[0];      final int y = meeting[1];      final int time = meeting[2];      timeToPairs.putIfAbsent(time, new ArrayList<>());      timeToPairs.get(time).add(new Pair<>(x, y));    }     for (List<Pair<Integer, Integer>> pairs : timeToPairs.values()) {      Set<Integer> peopleUnioned = new HashSet<>();      for (Pair<Integer, Integer> pair : pairs) {        final int x = pair.getKey();        final int y = pair.getValue();        uf.unionByRank(x, y);        peopleUnioned.add(x);        peopleUnioned.add(y);      }      for (final int person : peopleUnioned)        if (!uf.connected(person, 0))          uf.reset(person);    }     for (int i = 0; i < n; ++i)      if (uf.connected(i, 0))        ans.add(i);     return ans;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗