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

6-ljedd2 #141

Merged
merged 5 commits into from
Sep 25, 2024
Merged

6-ljedd2 #141

merged 5 commits into from
Sep 25, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Aug 3, 2024

πŸ”— 문제 링크

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μ½”λ”©ν…ŒμŠ€νŠΈ 고득점 Kit - 완전탐색(DFS) | ν”Όλ‘œλ„

문제 μ„€λͺ…

  • ν”Όλ‘œλ„ μ‹œμŠ€ν…œ(0 μ΄μƒμ˜ μ •μˆ˜λ‘œ ν‘œν˜„ν•©λ‹ˆλ‹€) :일정 ν”Όλ‘œλ„λ₯Ό μ‚¬μš©ν•΄μ„œ λ˜μ „μ„ νƒν—˜ν•  수 μžˆλ‹€.
  • 각 λ˜μ „λ§ˆλ‹€ νƒν—˜μ„ μ‹œμž‘ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„"와 λ˜μ „ νƒν—˜μ„ λ§ˆμ³€μ„ λ•Œ μ†Œλͺ¨λ˜λŠ” "μ†Œλͺ¨ ν”Όλ‘œλ„"κ°€ μžˆλ‹€.
    • "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„"λŠ” ν•΄λ‹Ή λ˜μ „μ„ νƒν—˜ν•˜κΈ° μœ„ν•΄ 가지고 μžˆμ–΄μ•Ό ν•˜λŠ” μ΅œμ†Œν•œμ˜ ν”Όλ‘œλ„λ₯Ό λ‚˜νƒ€λ‚΄λ©°,
    • "μ†Œλͺ¨ ν”Όλ‘œλ„"λŠ” λ˜μ „μ„ νƒν—˜ν•œ ν›„ μ†Œλͺ¨λ˜λŠ” ν”Όλ‘œλ„λ₯Ό λ‚˜νƒ€λ‚Έλ‹€.
    • 예λ₯Ό λ“€μ–΄ "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„"κ°€ 80, "μ†Œλͺ¨ ν”Όλ‘œλ„"κ°€ 20인 λ˜μ „μ„ νƒν—˜ν•˜κΈ° μœ„ν•΄μ„œλŠ” μœ μ €μ˜ ν˜„μž¬ 남은 ν”Όλ‘œλ„λŠ” 80 이상 이어야 ν•˜λ©°, λ˜μ „μ„ νƒν—˜ν•œ ν›„μ—λŠ” ν”Όλ‘œλ„ 20이 μ†Œλͺ¨λœλ‹€.
  • ν•œ μœ μ €κ°€ 였늘 이 λ˜μ „λ“€μ„ μ΅œλŒ€ν•œ 많이 νƒν—˜ν•˜λ € ν•œλ‹€.
  • μœ μ €μ˜ ν˜„μž¬ ν”Όλ‘œλ„ k와 각 λ˜μ „λ³„ "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„", "μ†Œλͺ¨ ν”Όλ‘œλ„"κ°€ λ‹΄κΈ΄ 2차원 λ°°μ—΄ dungeons κ°€ λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§ˆ λ•Œ, μœ μ €κ°€ νƒν—˜ν• μˆ˜ μžˆλŠ” μ΅œλŒ€ λ˜μ „ 수λ₯Ό return

μ œν•œμ‚¬ν•­

  • kλŠ” 1 이상 5,000 μ΄ν•˜μΈ μžμ—°μˆ˜
  • dungeons의 μ„Έλ‘œ(ν–‰) 길이(즉, λ˜μ „μ˜ 개수)λŠ” 1 이상 8 μ΄ν•˜
  • dungeons의 κ°€λ‘œ(μ—΄) κΈΈμ΄λŠ” 2
  • dungeons의 각 행은 각 λ˜μ „μ˜ ["μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„", "μ†Œλͺ¨ ν”Όλ‘œλ„"]
    • "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„"λŠ” 항상 "μ†Œλͺ¨ ν”Όλ‘œλ„"보닀 ν¬κ±°λ‚˜ κ°™μŠ΅λ‹ˆλ‹€.
    • "μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„"와 "μ†Œλͺ¨ ν”Όλ‘œλ„"λŠ” 1 이상 1,000 μ΄ν•˜μΈ μžμ—°μˆ˜μž…λ‹ˆλ‹€.
    • μ„œλ‘œ λ‹€λ₯Έ λ˜μ „μ˜ ["μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„", "μ†Œλͺ¨ ν”Όλ‘œλ„"]κ°€ μ„œλ‘œ 같을 수 μžˆλ‹€.

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

1μ‹œκ°„ + @

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

1. 문제 풀이

λ¬Έμ œμ—μ„œ 주어진 μž…λ ₯λ“€κ³Ό 쑰건에 따라 ν•„μš”ν•œ ν”Όλ‘œλ„μ™€ ν˜„μž¬ ν”Όλ‘œλ„λ₯Ό 계속 λ”ν•˜κ³  λΉΌκ°€λ©΄μ„œ μ™„μ „ 탐색을 ν•˜λŠ” 방법을 λ– μ˜¬λ ΈμŠ΅λ‹ˆλ‹€.

image

쑰건에 λ§žμ§€ μ•Šλ‹€λ©΄ λ’€λ‘œ λ‹€μ‹œ λŒμ•„κ°€κ³  , 쑰건이 λ§žλ‹€λ©΄ 더 깊게 νƒμƒ‰ν•©λ‹ˆλ‹€.
이 λ•Œ, 이미 ν΄λ¦¬μ–΄ν•œ λ˜μ „μ€ λ‹€μ‹œ λ°©λ¬Έν•  수 μ—†μŠ΅λ‹ˆλ‹€.

이 κ³Όμ •μ—μ„œ 이전 λ…Έλ“œμ˜ μƒνƒœλ‘œ λ˜λŒμ•„κ°€κΈ° μœ„ν•΄, λ°©λ¬Έ μ—¬λΆ€ 및 λ°©λ¬Έ 횟수λ₯Ό 이전 λ…Έλ“œκΉŒμ§€ νƒμƒ‰ν–ˆμ„ λ•Œμ˜ κ°’μœΌλ‘œ 볡ꡬλ₯Ό ν•΄μ•Όν•˜κΈ° λ•Œλ¬Έμ— visited 배열을 μΆ”κ°€λ‘œ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

nonlocal을 μ‚¬μš©ν•˜λ©΄ ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œλ§Œ 값을 κ°±μ‹ ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (λΉ„μ „μ—­λ³€μˆ˜)
μ΅œμ’…μ μœΌλ‘œ μΉ΄μš΄νŒ…λœ μ΅œλŒ€ νšŸμˆ˜κ°€ κ³„μ‚°λ˜μ–΄ 좜λ ₯λ©λ‹ˆλ‹€.

# 첫 번째 μ½”λ“œ 
def solution(k, dungeons):
    
    def DFS(k, cnt):
        nonlocal answer
    
        answer = max(answer, cnt)
        for i in range(len(dungeons)):

            # λ˜μ „ = ["μ΅œμ†Œ ν•„μš” ν”Όλ‘œλ„", "μ†Œλͺ¨ ν”Όλ‘œλ„"] 
            min_fatigue, use_fatigue = dungeons[i][0], dungeons[i][1]

            # ν˜„μž¬ ν”Όλ‘œλ„κ°€ ν•΄λ‹Ή λ˜μ „μ„ λ°©λ¬Έν•˜κΈ° μœ„ν•œ μ΅œμ†Œ ν”Όλ‘œλ„λ³΄λ‹€ 클 λ•Œ
            if not visited[i] and k >= min_fatigue:    
                visited[i] = True

                # λ°±νŠΈλž˜ν‚Ή : 이전 λ…Έλ“œλ‘œ λ‹€μ‹œ backν•  λ•Œ, 
                # ν•΄λ‹Ή λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜κΈ° μ „μ˜ ν”Όλ‘œλ„λ‘œ λ‹€μ‹œ 볡ꡬ
                DFS(k-use_fatigue, cnt+1)
                visited[i] = False

    answer = 0
    visited = [False] * len(dungeons) # visited 
    DFS(k, 0)     
    
    return answer

μ½”λ“œλ₯Ό 쑰금 더 효율적으둜 μž‘μ„±ν•  수 μžˆμ„κΉŒ κ³ λ―Όν•˜λ‹€κ°€
enumerate() λ₯Ό μ‚¬μš©ν•˜λ©΄ 각 λ˜μ „μ˜ μΈλ±μŠ€μ™€ 값을 λ™μ‹œμ— κ°€μ Έμ˜¬ 수 μžˆμ–΄μ„œ
λ°”λ‘œ μ μš©μ„ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
λ³€μˆ˜ 선언을 λ”°λ‘œ 해주지 μ•Šμ•„λ„ 반볡문으둜 λ°”λ‘œ μ μš©ν•΄λ³Ό 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

# enumerate()λ₯Ό ν™œμš©ν•˜μ—¬ 각 λ˜μ „μ˜ μΈλ±μŠ€μ™€ 값을 λ™μ‹œμ— 반볡
for i, (min_fatigue, use_fatigue) in enumerate(dungeons):
    if not visited[i] and k >= min_fatigue:
        visited[i] = True
        dfs(k - use_fatigue, cnt + 1)
        visited[i] = False

2. κ°œμ„ λœ μ½”λ“œ

def solution(k, dungeons):
    def dfs(k, cnt):
        #  ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ 값을 κ°±μ‹ ν•  수 μžˆλŠ” λΉ„μ „μ—­ λ³€μˆ˜λ‘œ μ„ μ–Έ 
        nonlocal answer
        
        answer = max(answer, cnt)
        
        # enumerate()λ₯Ό ν™œμš©ν•˜μ—¬ 각 λ˜μ „μ˜ μΈλ±μŠ€μ™€ 값을 λ™μ‹œμ— 반볡
        for i, (min_fatigue, use_fatigue) in enumerate(dungeons):
            
            if not visited[i] and k >= min_fatigue:
                
                visited[i] = True
                dfs(k - use_fatigue, cnt + 1)
                visited[i] = False
    
    answer = 0
    visited = [False] * len(dungeons)
    dfs(k, 0)
    
    return answer

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

@jung0115
Copy link
Member

jung0115 commented Aug 7, 2024

class Solution {
    static boolean[] visited;
    static int max = 0;
    public int solution(int k, int[][] dungeons) {
        
        visited = new boolean[dungeons.length];
        checkDungeon(dungeons, k, 0);
        
        return max;
    }
    static void checkDungeon(int[][] dungeons, int k, int cnt) {
        if(max < cnt) max = cnt;
        
        for(int i = 0; i < dungeons.length; i++) {
            if(!visited[i] && dungeons[i][0] <= k) {
                visited[i] = true;
                checkDungeon(dungeons, k - dungeons[i][1], cnt + 1);
                visited[i] = false;
            }
        }
    }
}

이전에 ν’€μ—ˆλ˜ μ½”λ“œμΈλ° 정은 λ‹˜ ν’€μ΄λž‘ μ™„μ „ λ˜‘κ°™μ΄ ν•΄κ²°ν–ˆμ—ˆλ„€μš”...!! ν•œ 번 더 문제 λ³΅κΈ°ν•˜λ©΄μ„œ 풀이 κ³ λ―Όν•΄λ³Ό 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹Ή 이번 μ°¨μ‹œλ„ μˆ˜κ³ ν•˜μ…¨μ–΄μš”!

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

저도 λͺ¨λ“  경우의 수λ₯Ό νƒμƒ‰ν•˜λŠ” 완전탐색 μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ 문제λ₯Ό ν’€μ—ˆμŠ΅λ‹ˆλ‹€.
μ €λŠ” κ°€λŠ₯ν•œ λ˜μ „ 방문의 μˆœμ„œλ₯Ό μ–»κΈ° μœ„ν•΄μ„œ μˆœμ—΄μ„ μ‚¬μš©ν–ˆλŠ”λ° 이번 문제λ₯Ό ν’€λ©΄μ„œ μˆœμ—΄ 라이브러리λ₯Ό μ“°λŠ” 방법을 μ •ν™•ν•˜κ²Œ μ•Œκ²Œ λ˜μ—ˆλ„€μš”!

from itertools import permutations

def solution(k, dungeons):
    answer = 0

    # λͺ¨λ“  λ˜μ „ λ°©λ¬Έ μˆœμ„œλ₯Ό 생성
    for order in permutations(range(len(dungeons))):
        remaining_energy = k
        visited_count = 0

        # ν˜„μž¬ μˆœμ„œμ— λ”°λ₯Έ λ˜μ „ λ°©λ¬Έ μ‹œλ„
        for dungeon_index in order:
            min_energy, cost = dungeons[dungeon_index]
            if remaining_energy >= min_energy:
                visited_count += 1
                remaining_energy -= cost

        # λ°©λ¬Έν•œ λ˜μ „μ˜ 수λ₯Ό 기둝
        answer = max(answer, visited_count)

    return answer

μ •μ€λ‹˜μ˜ μˆ˜λ„μ½”λ“œ 덕뢄에 enumerate()λ₯Ό μ μš©ν•΄μ„œ 더 효율적으둜 μ½”λ“œλ₯Ό 지 수 μžˆλ‹€λŠ” 것도 μ•Œκ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€πŸ˜Š

이번 PR도 μˆ˜κ³ ν•˜μ…¨μ–΄μš”!

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