diff --git a/FindCelebrity.java b/FindCelebrity.java new file mode 100644 index 0000000..dea3605 --- /dev/null +++ b/FindCelebrity.java @@ -0,0 +1,27 @@ +//TC-O(n^2) SC-O(N) +public class FindCelebrity { + private int numberOfPeople; + public int findCelebrity(int n) { + numberOfPeople = n; + int celebrityCandidate = 0; + for (int i = 0; i < n; i++) { + if (knows(celebrityCandidate, i)) { + celebrityCandidate = i; + } + } + if (isCelebrity(celebrityCandidate)) { + return celebrityCandidate; + } + return -1; + } + + private boolean isCelebrity(int i) { + for (int j = 0; j < numberOfPeople; j++) { + if (i == j) continue; + if (knows(i, j) || !knows(j, i)) { + return false; + } + } + return true; + } +} diff --git a/OptimizeWaterDistributionInVillage.java b/OptimizeWaterDistributionInVillage.java new file mode 100644 index 0000000..7ab78da --- /dev/null +++ b/OptimizeWaterDistributionInVillage.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class OptimizeWaterDistributionInVillage { + int[] parent; + public int minCostToSupplyWater(int n, int[] wells, int[][] pipes) { + List edges=new ArrayList<>(); + parent=new int[n+1]; + for(int i=0;i a[2]-b[2]); + + //kruskal's algorithm starts + + int cost = 0; + + for(int[] edge : edges){ + System.out.println(edge[0]+"--"+edge[1]+"--"+edge[2]); + int x = Union(edge[0]); + int y = Union(edge[1]); + + if(x != y){ + cost += edge[2]; + parent[y] = x; + } + } + return cost; + } + + + private int Union(int val){ + + if(val != parent[val]){ + parent[val] = Union(parent[val]); + } + + return parent[val]; + + } +}