Skip to content

Commit

Permalink
Merge pull request #141 from AlgoLeadMe/6-ljedd2-02
Browse files Browse the repository at this point in the history
6-ljedd2
  • Loading branch information
LJEDD2 authored Sep 25, 2024
2 parents 62a07bd + 9ed98a6 commit 016a501
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions LJEDD2/2024-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
| 3차시 | 2024.07.24 | 깊이/너비 우선 탐색(DFS/BFS) | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/43164">여행경로</a> | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/131) |
| 4차시 | 2024.07.27 | 그래프 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/49189">가장 먼 노드</a> | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/133) |
| 5차시 | 2024.07.31 | 다이나믹프로그래밍 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/43105">정수 삼각형</a> | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/139) |
| 6차시 | 2024.08.03 | 완전탐색 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/87946">피로도</a> | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/141) |
49 changes: 49 additions & 0 deletions LJEDD2/2024-2/완전탐색/피로도..py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 첫 번째 코드
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


# 개선된 코드
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

0 comments on commit 016a501

Please sign in to comment.