Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb95350d

Browse files
committed
1584. Min Cost to connect all points in Java
1 parent52bdc09 commitb95350d

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
classEdge {
2+
publicintsource;
3+
publicintdestination;
4+
publicintweight;// manhattan distance
5+
6+
publicEdge(ints,intd,intw){
7+
this.source =s;
8+
this.destination =d;
9+
this.weight =w;
10+
}
11+
12+
@Override
13+
publicStringtoString() {
14+
return"Edge(u=" +source +", v=" +destination +", w=" +weight +")";
15+
}
16+
}
17+
18+
classDisjointSet {
19+
privateint[]roots;
20+
privateint[]ranks;
21+
22+
publicDisjointSet(intn) {
23+
this.roots =newint[n];
24+
this.ranks =newint[n];
25+
26+
// intializing
27+
for(inti=0;i<n;i++){
28+
this.roots[i] =i;
29+
this.ranks[i] =0;// initially ranking 0
30+
}
31+
}
32+
33+
// get the root, use path compression
34+
publicintfind(intu) {
35+
if(this.roots[u] ==u)
36+
returnu;
37+
38+
introot =find(this.roots[u]);
39+
this.roots[u] =root;
40+
returnroot;
41+
}
42+
43+
// use ranking
44+
publicvoidunion(intx,inty) {
45+
introotX =find(x),rootY =find(y);
46+
intrankX =this.ranks[rootX],rankY =this.ranks[rootY];
47+
48+
if(rootX ==rootY)// already same roots
49+
return;
50+
51+
if(rankX <rankY){
52+
// put into rootY
53+
this.roots[rootX] =this.roots[rootY];
54+
this.ranks[rootY]++;
55+
}
56+
else{
57+
// default, put into rootX
58+
this.roots[rootY] =this.roots[rootX];
59+
this.ranks[rootX]++;
60+
}
61+
}
62+
63+
publicbooleanareDisjoint(intu,intv) {
64+
return (find(u) !=find(v));
65+
}
66+
}
67+
68+
classSolution {
69+
privateintgetManhattanDistance(int[]p1,int[]p2){
70+
return (
71+
Math.abs(p1[0] -p2[0]) +
72+
Math.abs(p1[1] -p2[1])
73+
);
74+
}
75+
76+
privateList<Edge>getEdges(int[][]points) {
77+
intn =points.length;
78+
List<Edge>edges =newArrayList<>();
79+
80+
// edge case
81+
if(n <=1)
82+
returnedges;
83+
84+
for(inti=0;i<(n -1);i++){
85+
for(intj=i+1;j<n;j++){
86+
intw =getManhattanDistance(points[i],points[j]);
87+
edges.add(newEdge(i,j,w));
88+
}
89+
}
90+
91+
returnedges;
92+
}
93+
94+
privatebooleanisEdgeCase(int[][]points) {
95+
return (points.length <=1);
96+
}
97+
98+
privateintgetCostMST(List<Edge>edges,intnumVertices) {
99+
DisjointSetds =newDisjointSet(numVertices);
100+
intminCost =0,numEdgesTaken =0;
101+
102+
for(Edgeedge:edges){
103+
if(ds.areDisjoint(edge.source,edge.destination)){
104+
ds.union(edge.source,edge.destination);
105+
numEdgesTaken++;
106+
minCost +=edge.weight;
107+
}
108+
109+
if(numEdgesTaken == (numVertices -1))// tree is formed, early exit
110+
break;
111+
}
112+
returnminCost;
113+
}
114+
115+
publicintminCostConnectPoints(int[][]points) {
116+
// edge cases
117+
if(isEdgeCase(points))
118+
return0;
119+
120+
// edges
121+
List<Edge>edges =getEdges(points);
122+
123+
// sort the edges in ascending order
124+
edges.sort((x1,x2) -> (x1.weight -x2.weight));
125+
126+
// Kruskals algorithm for MST [Union Find]
127+
returngetCostMST(edges,points.length);
128+
}
129+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp