팡세영
Log sey
팡세영
전체 방문자
오늘
어제
  • 분류 전체보기 (73)
    • PS (45)
      • programmers (13)
      • 백준 (29)
    • Android (16)
    • Daily (0)
    • Kotlin (6)
    • Design Pattern (1)
    • Java (1)
    • Flutter (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 백준
  • 골드
  • BFS
  • 자바
  • 코틀린
  • programmers
  • 의존성 주입
  • LEVEL2
  • 이분탐색
  • mvvm
  • compose
  • flutter
  • programmers #프로그래머스
  • ArcitecturePattern
  • java
  • 하단네비게이션바
  • TestCode
  • 해쉬맵
  • 프로그래머스
  • 실버
  • 정렬
  • Kotlin
  • CustomView
  • 구현
  • Android
  • binding
  • 안드로이드
  • 문자열
  • 완전탐색
  • DFS

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
팡세영

Log sey

Programmers - 피로도 (Java) 완전탐색
PS/programmers

Programmers - 피로도 (Java) 완전탐색

2022. 8. 10. 18:26

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


[문제풀이] 

입력으로 들어오는 배열 길이가 최대 8이므로 완전탐색으로 해결 할 수 있습니다.

 

우선 저는 순열을 이용해 모든 경우의 수를 구하여 문제를 해결하였습니다.

 

class Solution {
    static int max = Integer.MIN_VALUE;

    public static int solution(int k, int[][] dungeons) {

        Solution.permutation(dungeons,0, dungeons.length,dungeons.length,k);

        return max;
    }


    static void permutation(int[][] arr, int depth, int n, int r, int k) {
        if (depth == r) {
            check(arr, r, k);
            return;
        }

        for (int i=depth; i<n; i++) {
            swap(arr, depth, i);
            permutation(arr, depth + 1, n, r,k);
            swap(arr, depth, i);
        }
    }

    static void swap(int[][] arr, int depth, int i) {
        int minimum = arr[depth][0];
        arr[depth][0] = arr[i][0];
        arr[i][0] = minimum;

        int need = arr[depth][1];
        arr[depth][1] = arr[i][1];
        arr[i][1] = need;

    }
    static void check(int[][] arr, int r, int userStamina) {

        int ableDungeons = 0;
        for (int i = 0; i < r; i++) {
            // System.out.print("["+arr[i][0] + "," + arr[i][1] + "] " );
            if(userStamina >= arr[i][0]){
                userStamina -= arr[i][1];
                ableDungeons++;
            }
        }//System.out.println();

        if(ableDungeons > max) max = ableDungeons;
    }

}

 

밑의 코드는 다른 분이 푸신 코드인데요,

dfs를 이용해 완전탐색을 해서 문제를 해결하셨습니다.

class Solution {
    public static boolean check[];
    public static int ans = 0;

    public int solution(int k, int[][] dungeons) {
        check = new boolean[dungeons.length];

        dfs(k, dungeons, 0);

        return ans;
    }
    public static void dfs(int tired, int[][] dungeons, int cnt){
        for(int i=0; i<dungeons.length; i++){
            if(!check[i] && dungeons[i][0]<=tired){
                check[i] = true;
                dfs(tired-dungeons[i][1], dungeons, cnt+1);
                check[i] = false;
            }
        }
        ans = Math.max(ans, cnt);
    }
}

 

'PS > programmers' 카테고리의 다른 글

프로그래머스 - 개인정보 수집 유효기간  (0) 2023.01.18
프로그래머스 - 프렌즈 4블록  (0) 2022.08.30
[프로그래머스] 기능개발  (1) 2022.06.27
프로그래머스 타겟 넘버  (0) 2022.06.27
프로그래머스 두 개 뽑아서 더하기  (0) 2022.06.27
    'PS/programmers' 카테고리의 다른 글
    • 프로그래머스 - 개인정보 수집 유효기간
    • 프로그래머스 - 프렌즈 4블록
    • [프로그래머스] 기능개발
    • 프로그래머스 타겟 넘버
    팡세영
    팡세영
    Android, CS, PS

    티스토리툴바