728x90
    
    
  반응형
    
    
    
  
https://school.programmers.co.kr/learn/courses/30/lessons/87946
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
이 문제는 완전탐색 문제.
check 배열과 갯수 카운트만 잘 신경써주면 쉽게 풀 수 있다.
public void find(int dgCnt, int hp, int[][] dungeons, int cnt) {
    if(hp >= 0) {
        max = Math.max(max, cnt);
    }
    for (int i = 0; i < dungeons.length; i++) {
        if(!check[i]) {
            check[i] = true;
            int needHp = 0;
            if(dungeons[i][0] <= hp) {
                needHp = dungeons[i][1];
                find(dgCnt + 1, hp - needHp, dungeons, cnt + 1);
            }
            check[i] = false;
        }
    }
}
이런식으로 재귀를 구현하면 백트래킹(완전탐색) 구현 완성이다.
package backTracking.fatigue;
public class Fatigue {
	public static void main(String[] args) {
		Solution s = new Solution();
		int k = 80;
		int[][] dungeons = {{80,20},{50,40},{30,10}};
		System.out.println(s.solution(k, dungeons));
	}
}
class Solution {
	boolean[] check;
	int max = Integer.MIN_VALUE;
	int len;
	int hp;
	
    public int solution(int k, int[][] dungeons) {
        len = dungeons.length;
        check = new boolean[len];
        find(0, k, dungeons, 0);
        
        return max;
    }
    
    public void find(int dgCnt, int hp, int[][] dungeons, int cnt) {
    	if(hp >= 0) {
    		max = Math.max(max, cnt);
    	}
    	
    	for (int i = 0; i < dungeons.length; i++) {
			if(!check[i]) {
				check[i] = true;
				int needHp = 0;
				if(dungeons[i][0] <= hp) {
					needHp = dungeons[i][1];
					find(dgCnt + 1, hp - needHp, dungeons, cnt + 1);
				}
				check[i] = false;
			}
		}
    }
}728x90
    
    
  반응형
    
    
    
  '코딩테스트 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스/java] 거리두기 확인하기 - 카카오 기출 (0) | 2022.08.28 | 
|---|---|
| [프로그래머스/java] 행렬 테두리 회전하기 (0) | 2022.08.24 | 
| [프로그래머스/Oracle] 우유와 요거트가 담긴 장바구니 (0) | 2022.08.07 | 
| [프로그래머스/java] 방금그곡 - 2018 KAKAO BLIND RECRUITMENT (0) | 2022.04.05 | 
| [프로그래머스/java] 오픈채팅방 - 2019 KAKAO BLIND RECRUITMENT - 리스트(List)를 배열(Array)로 (0) | 2022.04.05 | 
										
									
										
									
										
									
										
									
댓글