From 0dc1a012e3a4dc2360cee1768396ee84949c81ca Mon Sep 17 00:00:00 2001 From: sourav2256 Date: Fri, 29 Dec 2023 12:01:39 +0530 Subject: [PATCH 1/2] add 2092. Find All People With Secret --- Java/Find_All_People_With_Secret.java | 81 +++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Java/Find_All_People_With_Secret.java diff --git a/Java/Find_All_People_With_Secret.java b/Java/Find_All_People_With_Secret.java new file mode 100644 index 00000000..15af8e3f --- /dev/null +++ b/Java/Find_All_People_With_Secret.java @@ -0,0 +1,81 @@ +class Solution { + public List findAllPeople(int n, int[][] meetings, int firstPerson) { + + // create map + Map> 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 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 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; + } + } +} \ No newline at end of file From 4750fb4f5c81381fa2b8f854b92534521f40e36e Mon Sep 17 00:00:00 2001 From: sourav2256 Date: Sat, 12 Oct 2024 19:31:41 +0530 Subject: [PATCH 2/2] add 299. Bulls and Cows --- Java/BullsAndCows.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Java/BullsAndCows.java diff --git a/Java/BullsAndCows.java b/Java/BullsAndCows.java new file mode 100644 index 00000000..463383da --- /dev/null +++ b/Java/BullsAndCows.java @@ -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"; + } +}