-
Notifications
You must be signed in to change notification settings - Fork 0
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
25-InSange #87
Conversation
μ.... |
There was a problem hiding this 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κ° λ°μ μλλ€.........
μ... λ¬Έμ λλ°μ΄λ€..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νΈλ¦¬ μ 체λ₯Ό λ³Ό νμ μμ΄ λ£¨νΈ λ Έλμ νλ³΄λ§ μ°ΎμΌλ©΄ λλκ΅°μ.. λͺ¨λ λ Έλκ° λ¦¬νλ Έλκ° λκΈ° μ μλ 리νλ Έλλ€μ λ£¨νΈ λ Έλκ° λ μ μλ€λ μ μ μ΄μ©ν΄ 리ν λ Έλλ€μ μ κ±°νλ€λ κΈλ°© λ°μμ΄ λ μ€λ₯΄μ§ μμ κ² κ°μλ° μ¬λ°λ νμ΄λ€μ γ γ
λ¬Έμ κ°μλ₯Ό μ μ΄μ£Όμλ λ무 νΈνλ€μ γ γ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ Έλκ° 2κ° μ΄νκ° λ λκΉμ§ 리νλ Έλλ₯Ό κ³μ μ κ±°νλ©΄ λ§μ§λ§μ λ¨λ κ°μ μ°ΎμΌλ©΄ κ°μ₯ κ°μ΄λ° μλ κ°μ΄ λκ² κ΅°μ©..μ΅μ λμ΄μ κ°λ μ΄ μ κΈ°μ΅μ΄ μ λμ μ’ ν€λ§Έλ€μ. κΉλν νμ΄ μ λ΄€μ΅λλΉ
π λ¬Έμ λ§ν¬
Minimum Height Trees
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
λ¬Έμ κ°μ
n
κ°μ λ Έλκ° μ£Όμ΄μ§λ©° κ° λ Έλλ λ¨μΌ κ²½λ‘λ‘ λμ΄ μλ€. μ¦, λ ΈλλΌλ¦¬ νλμ κ°μ λ§ μ‘΄μ¬νλ€.κ·Έλ¦¬κ³ νΈλ¦¬μ νΉμ± μ μνμ΄ μ‘΄μ¬νμ§ μλλ€.
λ£¨νΈ λ Έλμμ μμ λ ΈλκΉμ§ λλ¬νλ κ²μ νΈλ¦¬μ λμ΄μ΄λ€.
μ£Όμ΄μ§ λ Έλλ€ μ€μμ μμλ‘ λ£¨νΈλ₯Ό μ§μ νμ λ μ΅μ λμ΄λ₯Ό κ°μ§λ λ Έλλ€μ μΆλ ₯νλΌ.
νμ΄ μ κ·Ό λ°©μ
λ¬Έμ λ§ μ½μμ λ 보면 λͺ¨λ λ Έλλ€ μ€μμ μμμ λ Έλλ₯Ό νλμ© μ νν΄κ°λ©΄μ νΈλ¦¬μ λμ΄κ° μ΅μμΈ κ²λ€μ λͺ¨μΌλ©΄ λκ² λ€! λ‘ μ κ·Όνλ©΄ μκ° μ΄κ³Όκ° λ°μν κ²μ΄λ€.
κ·ΈλΌ μ΄λ»κ² νλ©΄ μ’μκΉ?
λ§μ½ λ Έλμ κ°μ
![image](https://private-user-images.githubusercontent.com/51250442/356687590-da80d16e-deb3-46b3-abef-3e9f4460accc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxOTI4MzgsIm5iZiI6MTczOTE5MjUzOCwicGF0aCI6Ii81MTI1MDQ0Mi8zNTY2ODc1OTAtZGE4MGQxNmUtZGViMy00NmIzLWFiZWYtM2U5ZjQ0NjBhY2NjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEwVDEzMDIxOFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ0ZThkYmRmZGQyMTIwZWNlZmVmZjIwMzNmMDhmMWY4NjEwM2JiOTk1MDc4YzVjNTJmMWU1MjI3MDlkODlhNDYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.DHO-0qAjvL3l_CBelic00ELFtwplNr8A-MMmDuGLYWw)
n
κ°κ° 2λΌλ©΄ νΈλ¦¬μ λμ΄λ 무쑰건 1μΌ μ λ°μ μλ€. μλ‘ μ°κ²°ν΄μ£Όλ κ°μ μ΄ ν κ° λΏμ΄κΈ° λλ¬Έμ΄λ€.κ·ΈλΌ λ Έλμ κ°μκ° 3μ΄λΌλ©΄? μλ§ 0-1-2 μ΄λ° μμΌλ‘ λ κ²μ΄κ³ μ΄ λ μμ μ€κ° 1μ μ νν΄μ£Όλ©΄ λμ΄κ° 1μ΄ λ κ²μ΄λ€.
κ·ΈλΌ λ§μ½ 4λΌλ©΄? 0-1-2-3 μΌλ‘ λμ΄ μλ€λ©΄ 1κ³Ό 2λ₯Ό μ ννμ λ λμ΄κ° 2λ‘ μ΅μκ° λ κ²μ΄λ€.
λ¬Έμ μμλ‘ μ£Όμ΄μ§ κ²μ νμΈν΄λ³΄μ.
κ·Έλ¦Όμ 보μλ§μ μκ² μ§λ§ 1μ λͺ¨λ λ Έλλ€μ΄ μ°κ²°λμ΄ μκΈ° λλ¬Έμ 1μ΄ μ΅μ λμ΄λ₯Ό λ§λ λ€λ κ²μ νμΈν μ μλ€.
μ¬κΈ°μ μ‘°κΈλ§ λ€λ₯΄κ² μ κ·Όν΄λ³΄λ©΄ λ£¨νΈ λ Έλμ μ°κ²°λλ λ Έλλ€μ λͺ¨λ μμλ Έλλ€λ‘μ¨ κ°μ μ΄ ν κ°λΌλ κ²μ νμ ν μ μλ€.
μ¦, λ κ° μ΄μμ λ Έλλ€μ΄ μ‘΄μ¬ν λ κ°μ μ΄ ν κ° λΏμΈ μμ λ Έλλ€μ μ κ±°ν΄λκ°λ€λ©΄ κ²°κ΅ κ°μ μ κ°μκ° κ°μ₯ μ κ² λλ μμμ λ Έλλ€μ΄ μμ‘΄ν κ²μ΄λΌλ κ²μ νμ ν μ μλ€.
degree
λ₯Ό μ¦κ°μν¨λ€.degree
κ°μ΄ 1μΌ κ²½μ°)μΌ λ ν΄λΉ λ Έλλ₯Ό λ€μ νλ‘ λ£μ΄ λ°λ³΅νλ€.π μλ‘κ² μκ²λ λ΄μ©