프로그래머스 피로도

2025. 4. 24. 16:24·코딩테스트/프로그래머스

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/87946

문제 풀이

dfs를 이용해 가능한 모든 경우의 수를 구하면서 가장 큰 depth를 구하도록 함

function dungeons_search(depth, k, dungeons, visited) {
    let max_depth = depth;
    for (let i = 0; i < dungeons.length; i++) {
        if (!visited[i] && dungeons[i][0] <= k) {
            visited[i] = true;
            const cal_k = k - dungeons[i][1];
            const current_depth = dungeons_search(depth + 1, cal_k, dungeons, visited);
            max_depth = Math.max(max_depth, current_depth);
            visited[i] = false;
        }
    }
    return max_depth;
}

function solution(k, dungeons) {
    let visited = new Array(dungeons.length).fill(false);
    return dungeons_search(0, k, dungeons, visited);
}

결론

solution내부에 dfs 함수를 정의하여 k, dungeons, visited같은 변수를 전역으로 설정할 경우 굳이 파라미터로 넘길 필요가 없음

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

프로그래머스 모의고사  (0) 2025.04.24
프로그래머스 [1차] 캐시  (0) 2025.04.24
프로그래머스 기능개발  (0) 2025.04.22
프로그래머스 의상  (0) 2025.04.14
프로그래머스 폰켓몬  (0) 2025.04.14
'코딩테스트/프로그래머스' 카테고리의 다른 글
  • 프로그래머스 모의고사
  • 프로그래머스 [1차] 캐시
  • 프로그래머스 기능개발
  • 프로그래머스 의상
의현
의현
개발은 즐거워
  • 의현
    UIHYEON BLOG
    의현
  • 전체
    오늘
    어제
    • 분류 전체보기 (116)
      • 프론트엔드 (47)
        • JavaScript (47)
        • TypeScript (0)
        • HTML (0)
        • CSS (0)
        • React (0)
      • 프로젝트 (2)
        • Task Flow (2)
      • 코딩테스트 (66)
        • Binary Search (2)
        • bfs (Breadth-first s.. (4)
        • dfs (Deapth-first se.. (1)
        • Greedy (1)
        • Dynamic Programming (1)
        • two pointer (4)
        • 구현 (2)
        • LIS(Longest Increasi.. (0)
        • 문자열 (3)
        • 자료구조 (4)
        • 비트마스크 (2)
        • 수학 (2)
        • 프로그래머스 (40)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
의현
프로그래머스 피로도
상단으로

티스토리툴바