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

7-janghw0126 #145

Merged
merged 4 commits into from
Sep 24, 2024
Merged

7-janghw0126 #145

merged 4 commits into from
Sep 24, 2024

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Aug 8, 2024

πŸ”— 문제 링크

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ | μ–‘κ³Ό λŠ‘λŒ€

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

1μ‹œκ°„

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

이번 λ¬Έμ œλŠ” DFS을 μ΄μš©ν•˜μ—¬ 탐색을 μˆ˜ν–‰ν•˜κ³ , μ–‘μ˜ 수λ₯Ό μ΅œλŒ€λ‘œ μœ μ§€ν•˜λ©΄μ„œ 탐색을 μ§„ν–‰ν•˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. 이 문제의 핡심은 ν˜„μž¬ μœ„μΉ˜μ—μ„œ μ–‘κ³Ό λŠ‘λŒ€μ˜ 수λ₯Ό μΆ”μ ν•˜λ©°, 쑰건에 따라 탐색을 μ€‘λ‹¨ν•˜κ±°λ‚˜ κ³„μ†ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€!

🧐 문제 μ ‘κ·Ό 방법

1. 문제 뢄석:

  • 문제 μ§€λ¬Έμ—μ„œλŠ” λŠ‘λŒ€μ˜ μˆ˜κ°€ μ–‘μ˜ μˆ˜μ™€ κ°™κ±°λ‚˜ 더 λ§Žμ•„μ§€λ©΄, 더 이상 탐색을 진행할 수 μ—†λ‹€. 라고 λͺ…μ‹œλ˜μ–΄ μžˆμ–΄, λŠ‘λŒ€μ˜ μˆ˜κ°€ μ–‘μ˜ 수λ₯Ό μ΄ˆκ³Όν•˜λŠ” κ²½λ‘œλŠ” νƒμƒ‰ν•˜μ§€ μ•ŠλŠ” 것이 μ€‘μš”ν•˜λ‹€κ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ DFSλ₯Ό μ‚¬μš©ν•΄ λͺ¨λ“  경둜λ₯Ό νƒμƒ‰ν•˜λ˜, λŠ‘λŒ€μ˜ μˆ˜κ°€ μ–‘μ˜ μˆ˜λ³΄λ‹€ λ§Žμ•„μ§€λŠ” μˆœκ°„ 탐색을 μ€‘λ‹¨ν•˜λŠ” 아이디어λ₯Ό μ΄μš©ν–ˆμŠ΅λ‹ˆλ‹€.
  • λ˜ν•œ, 루트 λ…Έλ“œλΆ€ν„° μΆœλ°œν•˜μ—¬ κ°€λŠ₯ν•œ λͺ¨λ“  경둜λ₯Ό νƒμƒ‰ν•˜λ©΄μ„œ μ–‘μ˜ 수λ₯Ό μ΅œλŒ€λ‘œ λ§Œλ“€ 수 μžˆλŠ” 방법을 μ°Ύμ•„μ•Ό ν•œλ‹€. λΌλŠ” 지문을 톡해, λͺ¨λ“  경둜λ₯Ό 탐색해야 ν•˜λŠ” λ¬Έμ œλΌλŠ” μ μ—μ„œ DFSκ°€ μ ν•©ν•˜λ‹€κ³  νŒλ‹¨ν–ˆμŠ΅λ‹ˆλ‹€.

2. DFS 탐색:

  • DFSλ₯Ό ν†΅ν•΄μ„œ 트리의 깊이λ₯Ό μš°μ„ μœΌλ‘œ νƒμƒ‰ν•˜λ©°, ν˜„μž¬ κ²½λ‘œμ—μ„œ μ–‘κ³Ό λŠ‘λŒ€μ˜ 수λ₯Ό μΆ”μ ν•©λ‹ˆλ‹€. 탐색 도쀑 λŠ‘λŒ€μ˜ μˆ˜κ°€ μ–‘μ˜ 수 이상이 되면 κ·Έ κ²½λ‘œλŠ” 더 이상 탐색할 ν•„μš”κ°€ μ—†μœΌλ―€λ‘œ μ€‘λ‹¨ν•©λ‹ˆλ‹€.
  • μ–‘μ˜ μˆ˜κ°€ μ΅œλŒ€κ°€ 될 λ•ŒκΉŒμ§€ λͺ¨λ“  κ°€λŠ₯ν•œ 경둜λ₯Ό νƒμƒ‰ν•˜κ³ , κ·Έμ€‘μ—μ„œ μ–‘μ˜ μˆ˜κ°€ κ°€μž₯ λ§Žμ€ 경우λ₯Ό κΈ°λ‘ν•©λ‹ˆλ‹€.

βœ… 문제 둜직

  1. μ΄ˆκΈ°ν™”:
  • 각 λ…Έλ“œμ˜ μžμ‹ λ…Έλ“œλ“€μ„ μ €μž₯ν•˜κΈ° μœ„ν•΄ child 리슀트λ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.
  • edgesλ₯Ό μˆœνšŒν•˜μ—¬ λΆ€λͺ¨-μžμ‹ 관계λ₯Ό child λ¦¬μŠ€νŠΈμ— μ €μž₯ν•©λ‹ˆλ‹€.
  1. DFS μž¬κ·€ ν•¨μˆ˜ μ •μ˜:
  • ν˜„μž¬ λ…Έλ“œμ˜ 번호, μ–‘μ˜ 수(sheep), λŠ‘λŒ€μ˜ 수(wolf), λ‹€μŒμ— 탐색할 수 μžˆλŠ” λ…Έλ“œλ“€μ˜ 리슀트(next_nodes)λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€.
  • ν˜„μž¬ μ–‘μ˜ μˆ˜κ°€ λŠ‘λŒ€λ³΄λ‹€ λ§Žμ„ λ•Œλ§Œ 탐색을 계속 μ§„ν–‰ν•©λ‹ˆλ‹€.
  • ν˜„μž¬ μ–‘μ˜ μˆ˜κ°€ μ΅œλŒ€μΌ 경우 max_countλ₯Ό κ°±μ‹ ν•©λ‹ˆλ‹€.
  • λ‹€μŒ 탐색 κ°€λŠ₯ν•œ λ…Έλ“œλ“€μ„ 반볡적으둜 νƒμƒ‰ν•©λ‹ˆλ‹€.
  1. 탐색 μ‹œμž‘:
  • 루트 λ…Έλ“œμΈ 0번 λ…Έλ“œμ—μ„œ 탐색을 μ‹œμž‘ν•˜λ©°, μ²˜μŒμ—λŠ” μ–‘ 1λ§ˆλ¦¬κ°€ μ£Όμ–΄μ§‘λ‹ˆλ‹€.
  • child[0]을 탐색할 수 μžˆλŠ” 초기 λ…Έλ“œλ‘œ μ„€μ •ν•˜κ³ , DFSλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€.
  1. μ΅œμ’… κ²°κ³Ό λ°˜ν™˜:
  • λͺ¨λ“  탐색이 λλ‚œ ν›„, max_count에 μ €μž₯된 μ΅œλŒ€ μ–‘μ˜ 수λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

πŸ’₯ νŠΈλŸ¬λΈ” μŠˆνŒ…

λŠ‘λŒ€ μˆ˜μ™€ μ–‘ 수 비ꡐ λΆ€λΆ„μ—μ„œ μ΄μŠˆκ°€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€. μ²˜μŒμ—λŠ” wolf > sheep μ‘°κ±΄μ—μ„œ 탐색을 μ€‘λ‹¨ν–ˆμœΌλ‚˜, ν…ŒμŠ€νŠΈ κ³Όμ •μ—μ„œ λŠ‘λŒ€μ˜ μˆ˜κ°€ μ–‘κ³Ό 같은 κ²½μš°μ—λ„ 탐색이 잘λͺ»λ˜λŠ” κ²½μš°κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ 문제의 쑰건에 λ§žμΆ”μ–΄ wolf >= sheep으둜 쑰건을 μˆ˜μ •ν•¨μœΌλ‘œμ¨, μ •ν™•ν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

πŸ‘©β€πŸ’» μ΅œμ’… μ½”λ“œ

# μ „μ—­ λ³€μˆ˜λ‘œ μ΅œλŒ€ μ–‘μ˜ 수λ₯Ό μ €μž₯
max_count = 0

def solution(info, edges):
    # 각 λ…Έλ“œμ˜ μžμ‹ λ…Έλ“œλ₯Ό μ €μž₯ν•  리슀트 μ΄ˆκΈ°ν™”
    child = [[] for _ in range(len(info))]

    # edges에 따라 λΆ€λͺ¨-μžμ‹ 관계λ₯Ό μ„€μ •
    for p, c in edges:
        child[p].append(c)

    # 깊이 μš°μ„  탐색(DFS)을 μˆ˜ν–‰ν•˜λŠ” μž¬κ·€ ν•¨μˆ˜ μ •μ˜
    def dfs(node, sheep, wolf, next_nodes):
        global max_count

        # λŠ‘λŒ€ μˆ˜κ°€ μ–‘ μˆ˜λ³΄λ‹€ λ§Žμ•„μ§€λ©΄ 탐색 쀑지
        if wolf >= sheep:
            return

        # μ΅œλŒ€ μ–‘μ˜ 수λ₯Ό κ°±μ‹ 
        max_count = max(max_count, sheep)

        # ν˜„μž¬ λ…Έλ“œμ˜ μžμ‹ λ…Έλ“œλ“€μ„ 탐색 κ°€λŠ₯ λ…Έλ“œ λ¦¬μŠ€νŠΈμ— μΆ”κ°€
        next_nodes.extend(child[node])

        # λ‹€μŒμœΌλ‘œ 이동할 수 μžˆλŠ” λ…Έλ“œλ“€μ„ 순회
        for next_node in next_nodes:
            # λ‹€μŒ λ…Έλ“œκ°€ ν˜„μž¬ μœ„μΉ˜κ°€ μ•„λ‹Œ κ²½μš°μ—λ§Œ 이동
            new_next_nodes = [n for n in next_nodes if n != next_node]
            # λ‹€μŒ λ…Έλ“œμ— λŠ‘λŒ€κ°€ μžˆλŠ” 경우
            if info[next_node]:
                dfs(next_node, sheep, wolf + 1, new_next_nodes)
            # λ‹€μŒ λ…Έλ“œμ— 양이 μžˆλŠ” 경우
            else:
                dfs(next_node, sheep + 1, wolf, new_next_nodes)

    # 루트 λ…Έλ“œ(0번)μ—μ„œ DFS μ‹œμž‘ (μ²˜μŒμ— μ–‘ 1λ§ˆλ¦¬κ°€ 있음)
    dfs(0, 1, 0, child[0])

    return max_count

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

이번 PRμ—μ„œλŠ” 경둜λ₯Ό νƒμƒ‰ν•˜λŠ” 도쀑에 쑰건을 μΆ”κ°€ν•˜μ—¬ λΆˆν•„μš”ν•œ 경둜λ₯Ό 미리 μ°¨λ‹¨ν•¨μœΌλ‘œμ¨ νš¨μœ¨μ„±μ„ λ†’μ΄λŠ” 것이 μ€‘μš”ν•˜λ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€.

Copy link
Contributor

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

이 λ¬Έμ œλŠ” μ›μΉ™μ μœΌλ‘œ λ°±νŠΈλž˜ν‚Ή/μ™„μ „νƒμƒ‰μœΌλ‘œ ν’€ 수 μ—†λŠ”λ°, μΉ΄μΉ΄μ˜€μ—μ„œ μ œμ‹œν•œ ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€κ°€ λΉˆμ•½ν•΄μ„œ 톡과가 λ©λ‹ˆλ‹€. ν˜œμ›λ‹˜μ΄ μ˜¬λ €μ£Όμ‹  μ½”λ“œλ„ μΌμ’…μ˜ 완전탐색인데, μ•„λž˜μ™€ 같은 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ— λŒ€ν•΄ μ‹œκ°„μ΄ˆκ³Όκ°€ λ°œμƒν•©λ‹ˆλ‹€.

info = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
edges = [[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [2, 6], [3, 7], [3, 8], [4, 9], [4, 10], [5, 11], [5, 12], [6, 13], [6, 14], [7, 15], [7, 16]]
Return = 17

μžμ„Έν•œ μ„€λͺ…은 μ–‘κ³Ό λŠ‘λŒ€ 풀이 λΈ”λ‘œκ·Έλ₯Ό ν™•μΈν•΄λ³΄μ‹œλ©΄ λ©λ‹ˆλ‹€ :)
μš”μ•½ν•˜μžλ©΄, μ •ν•΄λŠ” [λΉ„νŠΈλ§ˆμŠ€ν‚Ήμ„ μ‚¬μš©ν•΄ ν™•μž₯된 μƒνƒœ κ³΅κ°„μ—μ„œμ˜ flood fill]μž…λ‹ˆλ‹€.

def solution(info, edges):
    n = len(info)
    
    visited = [False] * (1 << n)
    graph = [[] for _ in range(n)]
    
    for p, c in edges:
        graph[p].append(c)
    
    answer = 0
    def dfs(mask: int = 1):
        nonlocal answer
        
        if visited[mask]:
            return
        
        visited[mask] = True
        sheep, wolf = 0, 0
        
        for node in range(n):
            if mask & (1 << node):
                sheep += (info[node] == 0)
                wolf += (info[node] == 1)
        
        if sheep <= wolf:
            return
        
        answer = max(answer, sheep)
        
        for parent in range(n):
            if not mask & (1 << parent):
                continue

            for child in graph[parent]:
                dfs(mask | (1 << child))
    
    dfs(1)
    
    return answer


# 깊이 μš°μ„  탐색(DFS)을 μˆ˜ν–‰ν•˜λŠ” μž¬κ·€ ν•¨μˆ˜ μ •μ˜
def dfs(node, sheep, wolf, next_nodes):
global max_count
Copy link
Member

Choose a reason for hiding this comment

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

리뷰가 λŠ¦μ–΄μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€ πŸ₯²

μ‚¬μ†Œν•œ λΆ€λΆ„μ΄μ§€λ§Œ ν˜œμ› λ‹˜ μ½”λ“œ 쀑에 max_countλ₯Ό μ „μ—­ λ³€μˆ˜λ‘œ μ„ μ–Έν•œ 뢀뢄이 μžˆμ–΄μ„œ μ „μ—­ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  ν’€μ΄ν•˜λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ΄€μŠ΅λ‹ˆλ‹€!! ν˜œμ› λ‹˜ μ½”λ“œλ₯Ό 일뢀 μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹Ή

def solution(info, edges):
    # 각 λ…Έλ“œμ˜ μžμ‹ λ…Έλ“œλ₯Ό μ €μž₯ν•  리슀트 μ΄ˆκΈ°ν™”
    child = [[] for _ in range(len(info))]

    # edges에 따라 λΆ€λͺ¨-μžμ‹ 관계λ₯Ό μ„€μ •
    for p, c in edges:
        child[p].append(c)

    # 깊이 μš°μ„  탐색(DFS)을 μˆ˜ν–‰ν•˜λŠ” μž¬κ·€ ν•¨μˆ˜ μ •μ˜
    def dfs(node, sheep, wolf, next_nodes):
        # λŠ‘λŒ€ μˆ˜κ°€ μ–‘ μˆ˜λ³΄λ‹€ λ§Žμ•„μ§€λ©΄ 탐색 쀑지
        if wolf >= sheep:
            return sheep - 1  # ν˜„μž¬ μ–‘μ˜ μˆ˜μ—μ„œ 1을 λΊ€ 값을 λ°˜ν™˜

        # ν˜„μž¬ λ…Έλ“œμ˜ μžμ‹ λ…Έλ“œλ“€μ„ 탐색 κ°€λŠ₯ λ…Έλ“œ λ¦¬μŠ€νŠΈμ— μΆ”κ°€
        next_nodes.extend(child[node])

        max_sheep = sheep  # ν˜„μž¬κΉŒμ§€μ˜ μ΅œλŒ€ μ–‘μ˜ 수

        # λ‹€μŒμœΌλ‘œ 이동할 수 μžˆλŠ” λ…Έλ“œλ“€μ„ 순회
        for next_node in next_nodes:
            # λ‹€μŒ λ…Έλ“œκ°€ ν˜„μž¬ μœ„μΉ˜κ°€ μ•„λ‹Œ κ²½μš°μ—λ§Œ 이동
            new_next_nodes = [n for n in next_nodes if n != next_node]
            # λ‹€μŒ λ…Έλ“œμ— λŠ‘λŒ€κ°€ μžˆλŠ” 경우
            if info[next_node]:
                max_sheep = max(max_sheep, dfs(next_node, sheep, wolf + 1, new_next_nodes))
            # λ‹€μŒ λ…Έλ“œμ— 양이 μžˆλŠ” 경우
            else:
                max_sheep = max(max_sheep, dfs(next_node, sheep + 1, wolf, new_next_nodes))

        return max_sheep  # νƒμƒ‰λœ μ–‘μ˜ μ΅œλŒ€ 수λ₯Ό λ°˜ν™˜

    # 루트 λ…Έλ“œ(0번)μ—μ„œ DFS μ‹œμž‘ (μ²˜μŒμ— μ–‘ 1λ§ˆλ¦¬κ°€ 있음)
    return dfs(0, 1, 0, child[0])

@LJEDD2 LJEDD2 removed their request for review September 7, 2024 14:03
@janghw0126 janghw0126 merged commit ca2184f into main Sep 24, 2024
@janghw0126 janghw0126 deleted the 7-janghw0126 branch September 24, 2024 01:14
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.

3 participants