[Java] 프로그래머스 문제풀이

Lee GaEun·2025년 2월 21일

[Java] 알고리즘

목록 보기
60/93

KT 에이블스쿨 대비 ( IDE 없이 문제 풀이 )


프로그래머스 타겟 넘버 문제 링크

#1

class Solution {
    static int answer = 0;
    
    public int solution(int[] numbers, int target) {
        
        cal(0, 0, numbers.length, target, numbers);
        
        return answer;
    }
    
    public static void cal(int sum, int i, int len, int target, int[] numbers) {
        if(i==len) {
            if(sum==target) answer++;
            return;
        }
        
        cal(sum+numbers[i], i+1, len, target, numbers);
        cal(sum-numbers[i], i+1, len, target, numbers);
        
    }
}
  • UI가 바뀌고 IDE를 안 쓰니까 쉬운 문제도 풀기가 어렵..

프로그래머스 네트워크 문제 링크

#1

class Solution {
    static boolean[] visited;
    
    public int solution(int n, int[][] computers) {
        int answer = 0;
        visited = new boolean[n];
        
        for(int i=0; i<n; i++) {
            if(!visited[i]) {
                visited[i] = true;
                answer += connect(computers, i, n);
            }
        }
        return answer;
    }
    
    public static int connect(int[][] computers, int level, int n) {
        for(int i=0; i<n; i++) {
            if(!visited[i] && computers[level][i] == 1) {
                visited[i] = true;
                connect(computers, i, n);
            }
        }
        return 1;
    }
}


프로그래머스 유연근무제 문제 링크

#1

class Solution {
    public int solution(int[] schedules, int[][] timelogs, int startday) {
        int answer = 0;
        for(int i=0; i<schedules.length; i++) {
            if(checkP(schedules[i], timelogs[i], startday)) answer++;
        }
        return answer;
    }
    
    public static boolean checkP(int start, int[] time, int day) {
        int end = start + 10;
        if(end%100 >= 60) {
            end-=60;
            end+=100;
        }
        
        for(int i=0; i<time.length; i++) {
            if(day%7 == 6 || day%7 == 0) {
                day++;
                continue;
            }
            day++;
            if(time[i] > end) return false;
        }
        
        return true;
    }
}


프로그래머스 소수 찾기 문제 링크

#1

class Solution {
    public int solution(int n) {
        int answer = 0;
        for(int i=2; i<=n; i++) {
            boolean a = true;
            for(int j=2; j<=i/j; j++) {
                if(i%j==0) {
                    a = false;
                    break;
                }
            }
            if(a) answer++;
        }
        return answer;
    }
}


프로그래머스 수박수박수박수박수박수? 문제 링크

#1

class Solution {
    public String solution(int n) {
        String answer = "";
        for(int i=1; i<=n; i++) {
            if(i%2==1) answer+="수";
            else answer+="박";
        }
        return answer;
    }
}


프로그래머스 덧칠하기 문제 링크

#1

import java.util.*;

class Solution {
    public int solution(int n, int m, int[] section) {
        int answer = 0;
        Deque<Integer> sec = new ArrayDeque<>();
        for(int i=0; i<section.length; i++) {
            sec.add(section[i]);
        }
        
        int now;
        while(sec.size() != 0) {
            now = sec.pollFirst();
            answer++;
            for(int j=1; j<m; j++) {
                if(sec.contains(now+j)) sec.pollFirst();
            }
        }
        return answer;
    }
}

profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글