From 08ab3ef423176b17f2c518f82cd250ae3cba8cb7 Mon Sep 17 00:00:00 2001 From: Mansi Singh <101468475+Mansi8874@users.noreply.github.com> Date: Sat, 27 Jan 2024 12:30:16 +0530 Subject: [PATCH] Create Solutions --- .../Solutions | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Day-29/Q2 : Min Cost to Connect All Points/Solutions diff --git a/Day-29/Q2 : Min Cost to Connect All Points/Solutions b/Day-29/Q2 : Min Cost to Connect All Points/Solutions new file mode 100644 index 0000000..136a0e0 --- /dev/null +++ b/Day-29/Q2 : Min Cost to Connect All Points/Solutions @@ -0,0 +1,62 @@ +class Solution { + class Edge{ + int src; + int dest; + int wt; + public Edge(int s,int d,int w){ + this.src=s; + this.dest=d; + this.wt=w; + } + } + class Pair implements Comparable{ + int node; + int cost; + public Pair(int s,int d){ + this.node=s; + this.cost=d; + } + public int compareTo(Pair p2){ + return this.cost-p2.cost; + } + } + public int minCostConnectPoints(int[][] points) { + ArrayList graph[]=new ArrayList[points.length]; + for(int i=0;i(); + } + for(int i=0;i graph[],int v) { + boolean vis[] = new boolean[v]; + PriorityQueue pq = new PriorityQueue<>(); + + pq.add(new Pair(0, 0)); //you can choose any pair. i choose 0 + int ans = 0; + while(pq.size() != 0){ + Pair curr = pq.remove(); + if(!vis[curr.node]){ + ans += curr.cost; + vis[curr.node] = true; + for(int i=0;i