반응형
https://leetcode.com/problems/min-cost-to-connect-all-points/
크루스칼 알고리즘을 이용하여 풀면 됩니다.
class Solution {
public:
vector<pair<int, pair<int, int>>> edges;
int p[1001] = {0};
int minCostConnectPoints(vector<vector<int>>& points) {
for(int i=0; i<points.size(); i++) {
for(int j=1; j<points.size(); j++) {
edges.push_back({abs(points[i][0] - points[j][0]) + abs(points[i][1] - points[j][1]), {i, j}});
}
}
sort(edges.begin(), edges.end());
int cnt = 0;
int cost = 0;
for(auto edge : edges) {
int a = edge.second.first;
int b = edge.second.second;
int c = edge.first;
int pa = findParent(a);
int pb = findParent(b);
if(pa != pb) {
p[pa] = pb;
cost += c;
if(++cnt == points.size() - 1) break;
}
}
return cost;
}
int findParent(int x) {
if(p[x] == 0) return x;
return p[x] = findParent(p[x]);
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1631 : Path With Minimum Effort (0) | 2022.04.28 |
---|---|
LeetCode 1202 : Smallest String With Swaps (0) | 2022.04.27 |
LeetCode 1433 : Check If a String Can Break Another String (0) | 2022.04.24 |
LeetCode 1396 : Design Underground System (0) | 2022.04.24 |
LeetCode 535 : Encode and Decode TinyURL (0) | 2022.04.23 |