Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 2092. Find All People With Secret #473

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Java/BullsAndCows.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class BullsAndCows {
public String getHint(String secret, String guess) {
int A = 0;
int B = 0;
int[] count1 = new int[10];
int[] count2 = new int[10];

for (int i = 0; i < secret.length(); ++i)
if (secret.charAt(i) == guess.charAt(i))
++A;
else {
++count1[secret.charAt(i) - '0'];
++count2[guess.charAt(i) - '0'];
}

for (int i = 0; i < 10; ++i)
B += Math.min(count1[i], count2[i]);

return String.valueOf(A) + "A" + String.valueOf(B) + "B";
}
}
81 changes: 81 additions & 0 deletions Java/Find_All_People_With_Secret.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class Solution {
public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {

// create <time, index> map
Map<Integer, List<Integer>> timeToIndexes = new TreeMap<>();
int m = meetings.length;
for (int i = 0; i < m; i++) {
timeToIndexes.putIfAbsent(meetings[i][2], new ArrayList<>());
timeToIndexes.get(meetings[i][2]).add(i);
}

UF uf = new UF(n);
// base
uf.union(0, firstPerson);

// for every time we have a pool of people that talk to each other
// if someone knows a secret proir to this meeting - all pool will too
// if not - reset unions from this pool
for (int time : timeToIndexes.keySet()) {
Set<Integer> pool = new HashSet<>();

for (int ind : timeToIndexes.get(time)) {
int[] currentMeeting = meetings[ind];
uf.union(currentMeeting[0], currentMeeting[1]);
pool.add(currentMeeting[0]);
pool.add(currentMeeting[1]);
}

// meeting that took place now should't affect future
// meetings if people don't know the secret
for (int i : pool) if (!uf.connected(0, i)) uf.reset(i);
}

// if the person is conneted to 0 - they know a secret
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) if (uf.connected(i,0)) ans.add(i);
return ans;
}

// regular union find
private static class UF {
int[] parent, rank;

public UF(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) parent[i] = i;
}

public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);

if (rootP == rootQ)
return;

if (rank[rootP] < rank[rootQ]) {
parent[rootP] = rootQ;
} else {
parent[rootQ] = rootP;
rank[rootP]++;
}
}

public int find(int p) {
while (parent[p] != p) {
p = parent[parent[p]];
}
return p;
}

public boolean connected(int p, int q) {
return find(p) == find(q);
}

public void reset(int p) {
parent[p] = p;
rank[p] = 0;
}
}
}