Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25-InSange #87

Merged
merged 5 commits into from
Aug 25, 2024
Merged

25-InSange #87

merged 5 commits into from
Aug 25, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Aug 9, 2024

πŸ”— 문제 링크

Minimum Height Trees

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

문제 κ°œμš”

n개의 λ…Έλ“œκ°€ 주어지며 각 λ…Έλ“œλŠ” 단일 경둜둜 λ˜μ–΄ μžˆλ‹€. 즉, λ…Έλ“œλΌλ¦¬ ν•˜λ‚˜μ˜ κ°„μ„ λ§Œ μ‘΄μž¬ν•œλ‹€.
그리고 트리의 νŠΉμ„± 상 μˆœν™˜μ΄ μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€.
루트 λ…Έλ“œμ—μ„œ μžμ‹ λ…Έλ“œκΉŒμ§€ λ„λ‹¬ν•˜λŠ” 것은 트리의 높이이닀.
주어진 λ…Έλ“œλ“€ μ€‘μ—μ„œ μž„μ˜λ‘œ 루트λ₯Ό μ§€μ •ν–ˆμ„ λ•Œ μ΅œμ†Œ 높이λ₯Ό κ°€μ§€λŠ” λ…Έλ“œλ“€μ˜ 좜λ ₯ν•˜λΌ.

풀이 μ ‘κ·Ό 방식

문제만 μ½μ—ˆμ„ λ•Œ 보면 λͺ¨λ“  λ…Έλ“œλ“€ μ€‘μ—μ„œ μž„μ˜μ˜ λ…Έλ“œλ₯Ό ν•˜λ‚˜μ”© μ„ νƒν•΄κ°€λ©΄μ„œ 트리의 높이가 μ΅œμ†ŒμΈ 것듀을 λͺ¨μœΌλ©΄ λ˜κ² λ‹€! 둜 μ ‘κ·Όν•˜λ©΄ μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν•  것이닀.

그럼 μ–΄λ–»κ²Œ ν•˜λ©΄ μ’‹μ„κΉŒ?

λ§Œμ•½ λ…Έλ“œμ˜ 개수 nκ°œκ°€ 2라면 트리의 λ†’μ΄λŠ” 무쑰건 1일 수 밖에 μ—†λ‹€. μ„œλ‘œ μ—°κ²°ν•΄μ£ΌλŠ” 간선이 ν•œ 개 뿐이기 λ•Œλ¬Έμ΄λ‹€.
그럼 λ…Έλ“œμ˜ κ°œμˆ˜κ°€ 3이라면? μ•„λ§ˆ 0-1-2 이런 μ‹μœΌλ‘œ 될 것이고 이 λ•Œ μ—­μ‹œ 쀑간 1을 선택해주면 높이가 1이 될 것이닀.
그럼 λ§Œμ•½ 4라면? 0-1-2-3 으둜 λ˜μ–΄ μžˆλ‹€λ©΄ 1κ³Ό 2λ₯Ό μ„ νƒν–ˆμ„ λ•Œ 높이가 2둜 μ΅œμ†Œκ°€ 될 것이닀.
문제 μ˜ˆμ‹œλ‘œ 주어진 것을 ν™•μΈν•΄λ³΄μž.
image
그림을 보자마자 μ•Œκ² μ§€λ§Œ 1에 λͺ¨λ“  λ…Έλ“œλ“€μ΄ μ—°κ²°λ˜μ–΄ 있기 λ•Œλ¬Έμ— 1이 μ΅œμ†Œ 높이λ₯Ό λ§Œλ“ λ‹€λŠ” 것을 확인할 수 μžˆλ‹€.

μ—¬κΈ°μ„œ 쑰금만 λ‹€λ₯΄κ²Œ 접근해보면 루트 λ…Έλ“œμ— μ—°κ²°λ˜λŠ” λ…Έλ“œλ“€μ€ λͺ¨λ‘ μžμ‹λ…Έλ“œλ“€λ‘œμ¨ 간선이 ν•œ κ°œλΌλŠ” 것을 νŒŒμ•…ν•  수 μžˆλ‹€.

즉, 두 개 μ΄μƒμ˜ λ…Έλ“œλ“€μ΄ μ‘΄μž¬ν•  λ•Œ 간선이 ν•œ 개 뿐인 μžμ‹ λ…Έλ“œλ“€μ„ μ œκ±°ν•΄λ‚˜κ°„λ‹€λ©΄ κ²°κ΅­ κ°„μ„ μ˜ κ°œμˆ˜κ°€ κ°€μž₯ 적게 λ˜λŠ” μ†Œμˆ˜μ˜ λ…Έλ“œλ“€μ΄ 생쑴할 κ²ƒμ΄λΌλŠ” 것을 νŒŒμ•…ν•  수 μžˆλ‹€.

  1. κ°„μ„ μœΌλ‘œ 이어져 μžˆλŠ” λ…Έλ“œλ“€μ„ μ„œλ‘œ 맡에 μ €μž₯μ‹œν‚€κ³  각 λ…Έλ“œμ˜ κ°„μ„  개수 degreeλ₯Ό μ¦κ°€μ‹œν‚¨λ‹€.
  2. 총 λ…Έλ“œμ˜ μˆ˜κ°€ 3개 이상일 경우 (2κ°œλŠ” 무쑰건 1) 맨 초기의 리프 λ…Έλ“œλ“€μ„ 큐에닀가 λ„£μ–΄μ€€λ‹€.
  3. 리프 λ…Έλ“œλ“€μ„ μ œκ±°ν•˜λ©΄μ„œ ν˜„μž¬ 남은 λ…Έλ“œ κ°œμˆ˜λ“€μ„ μ—…λ°μ΄νŠΈ μ‹œμΌœμ£Όκ³  ν•΄λ‹Ή λ¦¬ν”„λ…Έλ“œμ™€ μ—°κ²° 된 λ…Έλ“œλ“€μ˜ κ°„μ„  값을 κ°μ†Œ μ‹œμΌœμ€€λ‹€.
  4. 리프 λ…Έλ“œλ₯Ό μ œκ±°ν•˜κ²Œ 되면 ν•΄λ‹Ή λ…Έλ“œμ™€ μ—°κ²°λ˜μ–΄ 있던 간선듀은 λ‹€μ‹œ 본인듀이 리프 λ…Έλ“œκ°€ 될 κ°€λŠ₯성이 μžˆλ‹€. 간선을 μ—…λ°μ΄νŠΈ ν–ˆμ„ λ•Œ ν•΄λ‹Ή λ…Έλ“œκ°€ 리프 λ…Έλ“œ(degree값이 1일 경우)일 λ•Œ ν•΄λ‹Ή λ…Έλ“œλ₯Ό λ‹€μ‹œ 큐둜 λ„£μ–΄ λ°˜λ³΅ν•œλ‹€.
  5. κ·Έλ ‡κ²Œ 제거된 λ…Έλ“œλ“€μ„ λ‹€ 배제 μ‹œμΌ°μ„ 경우 λ‚¨μ•„μžˆλŠ” λ…Έλ“œλ“€μ΄ 루트λ₯Ό μ„ μ •ν•˜μ˜€μ„ λ•Œ μ΅œμ†Œ 높이λ₯Ό μ§€λ‹ˆκ²Œ λœλ‹€.
class Solution {
public:
    vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
        map<int, vector<int>> m;
        vector<int> ans;
        vector<bool> check(n, false);
        int* degree = new int[n] {0};

        for(vector<int> node:edges)
        {
            m[node[0]].push_back(node[1]);
            m[node[1]].push_back(node[0]);
            degree[node[0]]++;
            degree[node[1]]++;
        }
        int current_n = n;

        queue<int> q;
        if(current_n > 2)
        {
            for(int i = 0; i < n; i++)
            {
                if(degree[i] == 1 && check[i] == false)
                {
                    check[i] = true;
                    q.push(i);
                    current_n--;
                }
            }
        }
        while(current_n > 2 && !q.empty())
        {
            int size = q.size();
            
            for(int j = 0; j < size; j++)
            {
                int remove_n = q.front();
                q.pop();

                for(auto node : m[remove_n])
                {
                    degree[node]--;
                    if(degree[node] == 1)
                    {
                        q.push(node);
                        check[node] = true;
                        current_n--;
                    }
                }
            }
        }

        for(int i = 0; i < n; i++)
        {
            if(check[i] == false) ans.push_back(i);
        }

        return ans;
    }
};

image

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@tgyuuAn
Copy link
Member

tgyuuAn commented Aug 12, 2024

와....

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from collections import defaultdict, deque

class Solution:
    def findMinHeightTrees(self, n: int, edges: list[list[int]]) -> list[int]:
        if n == 1: return [0]

        graph_info = defaultdict(list)

        for edge in edges:
            node1, node2 = edge
            graph_info[node1].append(node2)
            graph_info[node2].append(node1)
        
        leaves = []
        for key, value in graph_info.items():
            if len(value) == 1: leaves.append(key)

        while n > 2:
            n -= len(leaves)
            temp = []
            for leaf in leaves:
                for neighbor_leaf in graph_info[leaf]:
                    graph_info[neighbor_leaf].remove(leaf)
                    if len(graph_info[neighbor_leaf]) == 1: temp.append(neighbor_leaf)
                del graph_info[leaf]

            leaves = temp
        return leaves

와 μ§„μ§œ...

저도 μ™„νƒμœΌλ‘œ 밖에 생각이 μ•ˆλ‚˜μ„œ..

이거 무쑰건 4μ–΅λ²ˆ 연산인데 μ‹œκ°„ 초과 λ‚˜λŠ”λ°?

ν•΄μ„œ 67/71 κΉŒμ§€λŠ” κ°”λŠ”λ°

λ„μ €νžˆ μ•ˆλ– μ˜¬λΌμ™€μ„œ ν’€μ΄λ΄€λŠ”λ° μ§„μ§œ λŒ€λ°•μ΄λ„€μš”......

μ΅œμ†Œ 높이 νŠΈλ¦¬λŠ” λ£¨νŠΈκ°€ 1개 ν˜Ήμ€ 2개 밖에 μ•ˆλœλ‹€.........

와... 문제 λŒ€λ°•μ΄λ‹€..

image

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

트리 전체λ₯Ό λ³Ό ν•„μš” 없이 루트 λ…Έλ“œμ˜ ν›„λ³΄λ§Œ 찾으면 λ˜λŠ”κ΅°μš”.. λͺ¨λ“  λ…Έλ“œκ°€ λ¦¬ν”„λ…Έλ“œκ°€ 되기 μ „μ—λŠ” λ¦¬ν”„λ…Έλ“œλ“€μ€ 루트 λ…Έλ“œκ°€ 될 수 μ—†λ‹€λŠ” 점을 μ΄μš©ν•΄ 리프 λ…Έλ“œλ“€μ„ μ œκ±°ν•œλ‹€λ‹ˆ 금방 λ°œμƒμ΄ λ– μ˜€λ₯΄μ§„ μ•Šμ„ 것 같은데 μž¬λ°ŒλŠ” ν’€μ΄λ„€μš” γ…‹γ…‹

문제 κ°œμš”λ₯Ό μ μ–΄μ£Όμ‹œλ‹ˆ λ„ˆλ¬΄ νŽΈν•˜λ„€μš” γ… γ… 

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ…Έλ“œκ°€ 2개 μ΄ν•˜κ°€ λ λ•ŒκΉŒμ§€ λ¦¬ν”„λ…Έλ“œλ₯Ό 계속 μ œκ±°ν•˜λ©΄ λ§ˆμ§€λ§‰μ— λ‚¨λŠ” 값을 찾으면 κ°€μž₯ κ°€μš΄λ° μžˆλŠ” 값이 되겠ꡰ용..μ΅œμ†Œ λ†’μ΄μ˜ κ°œλ…μ΄ 잘 기얡이 μ•ˆ λ‚˜μ„œ μ’€ ν—€λ§Έλ„€μš”. κΉ”λ”ν•œ 풀이 잘 λ΄€μŠ΅λ‹ˆλ‹Ή

@InSange InSange merged commit 617e119 into main Aug 25, 2024
1 check passed
@InSange InSange deleted the 25-InSange branch August 25, 2024 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants