- Notifications
You must be signed in to change notification settings - Fork2.4k
1584. Min Cost to connect all points in Java#663
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletionsjava/1584-Min-Cost-to-Connect-All-Points.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
class Edge { | ||
public int source; | ||
public int destination; | ||
public int weight; // manhattan distance | ||
public Edge(int s, int d, int w){ | ||
this.source = s; | ||
this.destination = d; | ||
this.weight = w; | ||
} | ||
@Override | ||
public String toString() { | ||
return "Edge(u=" + source + ", v=" + destination + ", w=" + weight + ")"; | ||
} | ||
} | ||
class DisjointSet { | ||
private int[] roots; | ||
private int[] ranks; | ||
public DisjointSet(int n) { | ||
this.roots = new int[n]; | ||
this.ranks = new int[n]; | ||
// intializing | ||
for(int i=0; i<n; i++){ | ||
this.roots[i] = i; | ||
this.ranks[i] = 0; // initially ranking 0 | ||
} | ||
} | ||
// get the root, use path compression | ||
public int find(int u) { | ||
if(this.roots[u] == u) | ||
return u; | ||
int root = find(this.roots[u]); | ||
this.roots[u] = root; | ||
return root; | ||
} | ||
// use ranking | ||
public void union(int x, int y) { | ||
int rootX = find(x), rootY = find(y); | ||
int rankX = this.ranks[rootX], rankY = this.ranks[rootY]; | ||
if(rootX == rootY) // already same roots | ||
return; | ||
if(rankX < rankY){ | ||
// put into rootY | ||
this.roots[rootX] = this.roots[rootY]; | ||
this.ranks[rootY]++; | ||
} | ||
else{ | ||
// default, put into rootX | ||
this.roots[rootY] = this.roots[rootX]; | ||
this.ranks[rootX]++; | ||
} | ||
} | ||
public boolean areDisjoint(int u, int v) { | ||
return (find(u) != find(v)); | ||
} | ||
} | ||
class Solution { | ||
private int getManhattanDistance(int[] p1, int[] p2){ | ||
return ( | ||
Math.abs(p1[0] - p2[0]) + | ||
Math.abs(p1[1] - p2[1]) | ||
); | ||
} | ||
private List<Edge> getEdges(int[][] points) { | ||
int n = points.length; | ||
List<Edge> edges = new ArrayList<>(); | ||
// edge case | ||
if(n <= 1) | ||
return edges; | ||
for(int i=0; i<(n - 1); i++){ | ||
for(int j=i+1; j<n; j++){ | ||
int w = getManhattanDistance(points[i], points[j]); | ||
edges.add(new Edge(i, j, w)); | ||
} | ||
} | ||
return edges; | ||
} | ||
private boolean isEdgeCase(int[][] points) { | ||
return (points.length <= 1); | ||
} | ||
private int getCostMST(List<Edge> edges, int numVertices) { | ||
DisjointSet ds = new DisjointSet(numVertices); | ||
int minCost = 0, numEdgesTaken = 0; | ||
for(Edge edge: edges){ | ||
if(ds.areDisjoint(edge.source, edge.destination)){ | ||
ds.union(edge.source, edge.destination); | ||
numEdgesTaken++; | ||
minCost += edge.weight; | ||
} | ||
if(numEdgesTaken == (numVertices - 1)) // tree is formed, early exit | ||
break; | ||
} | ||
return minCost; | ||
} | ||
public int minCostConnectPoints(int[][] points) { | ||
// edge cases | ||
if(isEdgeCase(points)) | ||
return 0; | ||
// edges | ||
List<Edge> edges = getEdges(points); | ||
// sort the edges in ascending order | ||
edges.sort((x1, x2) -> (x1.weight - x2.weight)); | ||
// Kruskals algorithm for MST [Union Find] | ||
return getCostMST(edges, points.length); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.