반응형

완전 탐색 문제로 접근하여 풀이

프로그래머스 : 피로도 Lv.2

 

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

 

프로그래머스

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

programmers.co.kr

 

class Solution {
    
    int answer = 0;
    
    public int solution(int k, int[][] dungeons) {
        int n = dungeons.length;
		int[][] output = new int[n][n];
		boolean[] visited = new boolean[n];
		
		perm(dungeons, output, visited, 0, n, n, k);
        
        return answer;
    }
    
    public void perm(int[][] dungeons, int[][] output, boolean[] visited, int depth, int n, int r, int k)   {
		
		if(depth == r) {
			int cnt = 0 ;
			
			for(int[] result : output) {
				if(k >= result[0]) {
					k-= result[1];
					cnt++;
				}
			}
			System.out.println();
			
			answer = Math.max(cnt, answer);
			return;
		}
		
		for(int i=0;i<n;i++) {
			if(!visited[i]) {
				visited[i] = true;
				output[depth][0] = dungeons[i][0];
				output[depth][1] = dungeons[i][1];
				perm(dungeons, output, visited, depth+1, n, r, k);
				visited[i] = false;
			}
		}
	}
}
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

대충 만든 자판  (0) 2024.03.14
체육복  (0) 2024.03.13
호텔 대실  (0) 2024.03.09
로또의 최고 순위와 최저 순위  (0) 2024.03.08
숫자 짝꿍  (0) 2024.02.25
반응형

연습 문제 유형

프로그래머스 : 호텔 대실 Lv.2

 

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

 

프로그래머스

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

programmers.co.kr

 

import java.util.PriorityQueue;

class Solution {
    public int solution(String[][] book_time) {
        PriorityQueue<Integer> in = new PriorityQueue<>();
		PriorityQueue<Integer> out = new PriorityQueue<>();
		
		int max = 0;
		int cnt = 1;
		
		for(String[] time : book_time) {
			in.offer(getMin(time[0]));
			out.offer(getMin(time[1]) + 10);
		}
		
		while(!in.isEmpty() && !out.isEmpty()) {
			int endTime = out.poll();
			cnt--;
			
			while(!in.isEmpty() && in.peek() < endTime) {
				in.poll();
				cnt++;
			}
			
			max = Math.max(max, cnt);
		}
        
        return max;
    }
    
    public int getMin(String str){
		String[] s = str.split(":");
		return Integer.valueOf(s[1]) + Integer.valueOf(s[0]) * 60;
	}
}
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

체육복  (0) 2024.03.13
피로도  (1) 2024.03.11
로또의 최고 순위와 최저 순위  (0) 2024.03.08
숫자 짝꿍  (0) 2024.02.25
실패율  (0) 2024.02.23
반응형

 

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		
		
		Scanner sc = new Scanner(System.in);
		
		int tc = sc.nextInt();
		
		for(int i=0;i<tc;i++){
			
			int n = sc.nextInt();
			int cnt =0;
			int[] dp = new int[n+1];
			
			
			for(int j=2;j<=n;j++){
				for(int k=1;k<=n;k++){
					if(j*k > n)continue;
					if(dp[j*k]==0){
						dp[j*k]=1;
					}else if(dp[j*k]==1){
						dp[j*k]=0;
					}
				}
			}
			
			for(int j=1;j<=n;j++){
				if(dp[j] == 0){
					cnt++;
				}
			}
			System.out.println(cnt);
		}
	}
}
반응형
반응형

 

2021 Dev-Matching: 웹 백엔드 개발자(상반기) 문제 

프로그래머스 : 로또의 최고 순위와 최저 순위 Lv 1

 

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

 

프로그래머스

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

programmers.co.kr

 

class Solution {
    public int[] solution(int[] lottos, int[] win_nums) {
        
        //int lottos[] = {44, 1, 0, 0, 31, 25};
		//int win_nums[] = { 31, 10, 45, 1, 6, 19};
		int result[] = {6,5,4,3,2,1};
		
		int sameNums = 0;
		int zeroNums = 0;
		
		for(int i=0;i<lottos.length;i++) {
			for(int j=0;j<win_nums.length;j++) {
				if(lottos[i] == win_nums[j]) sameNums++;
			}
			
			if(lottos[i] == 0) zeroNums++;
		}
		
		int idx1 = 0;
		int idx2 = 0;
		
		if(sameNums + zeroNums > 1) idx1 = sameNums + zeroNums -1;
		if(sameNums > 1) idx2 = sameNums -1;
        
        int answer[] = {result[idx1 ], result[idx2]};
        
        return answer;
    }
}
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

피로도  (1) 2024.03.11
호텔 대실  (0) 2024.03.09
숫자 짝꿍  (0) 2024.02.25
실패율  (0) 2024.02.23
추억 점수  (0) 2024.02.22
반응형

연습문제

프로그래머스: 숫자 짝꿍 Lv1

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

 

프로그래머스

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

programmers.co.kr

 

// 배열 SET 으로 [9~0] 카운트를 저장
// 각 배열 SET에 카운트 while문으로 일치할 경우 answer에 추가

class Solution {
    public String solution(String X, String Y) {
        String answer = "";
        
        StringBuilder sb = new StringBuilder();
        
        int[] xNums = new int[10];
		int[] yNums = new int[10];
		
		for(String x : X.split("")) {
			xNums[Integer.parseInt(x)]++;
		}
		
		for(String y : Y.split("")) {
			yNums[Integer.parseInt(y)]++;
		}
		
		for(int i=9; i>=0;i--) {
			while(xNums[i] > 0 && yNums[i] > 0) {
				sb.append(i);
				
				xNums[i]--;
				yNums[i]--;
			}
		}
		
		if("".equals(sb.toString())) answer = "-1";
		else if("0".equals(sb.toString().substring(0,1))) answer = "0";
		else answer = sb.toString();
        
        return answer;
    }
}
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

호텔 대실  (0) 2024.03.09
로또의 최고 순위와 최저 순위  (0) 2024.03.08
실패율  (0) 2024.02.23
추억 점수  (0) 2024.02.22
푸드 파이트 대회  (0) 2024.02.21

+ Recent posts