[Algorithm] ๐Ÿ๏ธํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์„ฌ ์—ฐ๊ฒฐํ•˜๊ธฐ

HaJingJingยท2021๋…„ 5์›” 18์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
43/119

0. ๋ฌธ์ œ

n๊ฐœ์˜ ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๋ฅผ ๊ฑด์„คํ•˜๋Š” ๋น„์šฉ(costs)์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ตœ์†Œ์˜ ๋น„์šฉ์œผ๋กœ ๋ชจ๋“  ์„ฌ์ด ์„œ๋กœ ํ†ตํ–‰ ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ์„ return ํ•˜๋„๋ก solution์„ ์™„์„ฑํ•˜์„ธ์š”.

๋‹ค๋ฆฌ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฑด๋„ˆ๋”๋ผ๋„, ๋„๋‹ฌํ•  ์ˆ˜๋งŒ ์žˆ์œผ๋ฉด ํ†ตํ–‰ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ด…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด A ์„ฌ๊ณผ B ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๊ณ , B ์„ฌ๊ณผ C ์„ฌ ์‚ฌ์ด์— ๋‹ค๋ฆฌ๊ฐ€ ์žˆ์œผ๋ฉด A ์„ฌ๊ณผ C ์„ฌ์€ ์„œ๋กœ ํ†ตํ–‰ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

**์ œํ•œ์‚ฌํ•ญ

  • ์„ฌ์˜ ๊ฐœ์ˆ˜ n์€ 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • costs์˜ ๊ธธ์ด๋Š” ((n-1) * n) / 2์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ž„์˜์˜ i์— ๋Œ€ํ•ด, costs[i][0] ์™€ costs[i][1]์—๋Š” ๋‹ค๋ฆฌ๊ฐ€ ์—ฐ๊ฒฐ๋˜๋Š” ๋‘ ์„ฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋“ค์–ด์žˆ๊ณ , costs[i][2]์—๋Š” ์ด ๋‘ ์„ฌ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋‹ค๋ฆฌ๋ฅผ ๊ฑด์„คํ•  ๋•Œ ๋“œ๋Š” ๋น„์šฉ์ž…๋‹ˆ๋‹ค.
  • ๊ฐ™์€ ์—ฐ๊ฒฐ์€ ๋‘ ๋ฒˆ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„ ๊ฐ™์€ ์—ฐ๊ฒฐ๋กœ ๋ด…๋‹ˆ๋‹ค. ์ฆ‰ 0๊ณผ 1 ์‚ฌ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๊ณผ 0์˜ ๋น„์šฉ์ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์„ฌ ์‚ฌ์ด์˜ ๋‹ค๋ฆฌ ๊ฑด์„ค ๋น„์šฉ์ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, ๋‘ ์„ฌ ์‚ฌ์ด์˜ ๊ฑด์„ค์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ๋ด…๋‹ˆ๋‹ค.
  • ์—ฐ๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ์„ฌ์€ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ
n costs return
4 [[0,1,1],[0,2,2],[1,2,5],[1,3,1],[2,3,8]] 4

1. ์•„์ด๋””์–ด

๐Ÿ’ก ์ตœ์†Œ๋ถ€ํ„ฐ ๊ฐ„์„  ์—ฐ๊ฒฐ
๐Ÿ’ก ์‚ฌ์ดํด ์ƒ์„ฑ X
๐Ÿ’ก ์ด์–ด์ง„ ๋…ธ๋“œ ํ‘œ์‹œ

2. ํ•ต์‹ฌ ํ’€์ด

1) ์ตœ์†Œ๋ถ€ํ„ฐ priorityQueue๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌ

class Edge implements Comparable<Edge> {
    int start, end, cost;
    
    public Edge(int start, int end, int cost){
        this.start = start;
        this.end = end;
        this.cost = cost;
    }
    
    @Override
    public int compareTo(Edge o){
        return this.cost - o.cost;
    }
}
...
pq.offer(new Edge(costs[i][0],costs[i][1],costs[i][2]));
...

2) ์‚ฌ์ดํด ์ƒ์„ฑ X
๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์„œ๋กœ ๊ฐ™์œผ๋ฉด X, ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด ๋ถ€๋ชจ ๋…ธ๋“œ ์ฐพ์Œ

...
if(find(e.start) == find(e.end)) continue;
...
public static int find(int p){
        if(parent[p] == p) return p;
        return parent[p] = find(parent[p]);
}

3) ์ด์–ด์ง„ ๋…ธ๋“œ ํ‘œ์‹œ

public static void union(int a, int b){
        int rootA = find(a);
        int rootB = find(b);
        
        if(rootA != rootB) parent[rootB] = rootA;
    }

3. ์ฝ”๋“œ

import java.util.*;
class Edge implements Comparable<Edge> {
    int start, end, cost;
    
    public Edge(int start, int end, int cost){
        this.start = start;
        this.end = end;
        this.cost = cost;
    }
    
    @Override
    public int compareTo(Edge o){
        return this.cost - o.cost;
    }
}
class Solution {
    static int[] parent;
    static PriorityQueue<Edge> pq;
    public int solution(int n, int[][] costs) {
        int answer = 0;
        parent = new int[n];
        pq = new PriorityQueue<Edge>();
        
        for(int i=0; i<n; i++)
            parent[i] = i;
        
        for(int i=0; i<costs.length; i++){
            pq.offer(new Edge(costs[i][0],costs[i][1],costs[i][2]));
        }
        
        while(!pq.isEmpty()){
            Edge e = pq.poll();
            
            if(find(e.start) == find(e.end)) continue;
            else {
                union(e.start, e.end);
                answer+=e.cost;
            }
        }
        return answer;
    }
    
    public static int find(int p){
        if(parent[p] == p) return p;
        return parent[p] = find(parent[p]);
    }
    
    public static void union(int a, int b){
        int rootA = find(a);
        int rootB = find(b);
        
        if(rootA != rootB) parent[rootB] = rootA;
    }
}

4. ๊ฒฐ๊ณผ

์„ฑ๊ณต~

profile
๐ŸŒฑ์ดˆ๋ณด ๊ฐœ๋ฐœ์ž๐ŸŒฑ

0๊ฐœ์˜ ๋Œ“๊ธ€