매일 Algorithm

신재원·2023년 3월 13일
0

Algorithm

목록 보기
64/243

프로그래머스 (LEVEL 2)

class Solution {
    // 던전이 최대 8개 dfs
    public int answer = 0;
    public boolean[] visit;
    public int solution(int k, int[][] dungeons) {
        // 던전의 갯수로 설정
        visit = new boolean [dungeons.length];

        dfs(0, k, dungeons);

        return answer;
    }

    void dfs(int depth, int k, int[][] dungeons){

        for(int i = 0; i < dungeons.length; i++){
            // visit = 방문하지않았고, 피로도(k) 보다 같거나 작을경우
            if(visit[i] == false && dungeons[i][0] <= k){
                visit[i] = true;

                // 재귀함수를 통해 모든경우를 순회한다.
                dfs(depth + 1, k - dungeons[i][1], dungeons);
                visit[i] = false;
            }
        }
        answer = Math.max(answer, depth);
    }
}

프로그래머스 (LEVEL 1)

import java.util.*;
class Solution {
    public int[] solution(long n) {
        // long 타입 String으로 변환
        String st = String.valueOf(n);
        int[] answer = new int[st.length()];
        
    
        
        for(int i = 0; i < st.length(); i++){
            answer[i] = (int)st.charAt(i) - '0';
        }
        int [] result = new int[answer.length];
        int index = 0;
        // 내림차순 정렬
        for(int j = answer.length - 1; j >= 0; j--){
            result[index] = answer[j];
            index++;
        }
        return result;
    }
}

프로그래머스 (LEVEL 1)

import java.util.*;
class Solution {
    public long solution(long n) {
        long answer = -1;
        // 루트 씌운 값이 같을경우
        if((long)Math.sqrt(n) * (long)Math.sqrt(n) == n){
            answer = ((long)Math.sqrt(n) + 1) * ((long)Math.sqrt(n) + 1);
        }
        return answer;
    }
}

0개의 댓글