반응형

2019 KAKAO BLIND RECRUITMENT 문제 

프로그래머스: 실패율 Lv1

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

 

프로그래머스

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

programmers.co.kr

 

주석에 포함된 내용으로

1) arrayList Sorting을 확인

2) HashMap Value 값으로 내림차순 정렬을 해주어야한다.

import java.util.*;

// 2019 KAKAO BLIND RECRUITMENT > 실패율
// 예제를 확인 시, 순차적으로 스테이지를 깨야하기 때문에
// 실패율의 분모 값은 이전 실패수 만큼 감소

/*****************************************************
 1. ArrayList Sorting 확인
 2. HashMap Value 값으로 내림차순 정렬
******************************************************/

class Solution {
    public int[] solution(int N, int[] stages) {
        int[] answer = {};
        
        int pCnt = stages.length;
	HashMap<Integer, Float> map = new HashMap<>();
	
	for(int i=1;i<=N;i++) {
		int stagesStop = 0;
		
		for(int j=0;j<stages.length;j++) {
			if(stages[j] == i) {
				stagesStop++;
			}
		}
		
		float calc = 0;
		if(pCnt != 0) {
			calc = (float)stagesStop/pCnt;
		} 
    
		pCnt -= stagesStop;

		map.put(i, calc);	
	}
	
	List<Integer> keys = new ArrayList<>(map.keySet());
	Collections.sort(keys, (v2, v1) -> (map.get(v1).compareTo(map.get(v2))));
	
	answer = keys.stream().mapToInt(i -> i).toArray();

        return answer;
    }
}

 

반응형

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

로또의 최고 순위와 최저 순위  (0) 2024.03.08
숫자 짝꿍  (0) 2024.02.25
추억 점수  (0) 2024.02.22
푸드 파이트 대회  (0) 2024.02.21
2016년  (0) 2020.08.31
반응형

연습문제 유형, 구현 방식으로 문제 접근하여 풀이

 

프로그래머스 : 추억 점수 (Lv.1)

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

 

프로그래머스

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

programmers.co.kr

import java.util.ArrayList;

class Solution {
    public int[] solution(String[] name, int[] yearning, String[][] photo) {
        ArrayList<Integer> ar = new ArrayList<>();
		
		for(int i=0;i<photo.length;i++) {
			int score = 0;
			
			for(int j=0;j<photo[i].length;j++) {
				for(int k=0;k<name.length;k++) {
					if(name[k].equals(photo[i][j])) {	
						score += yearning[k];
					}
				}
			}
			
			ar.add(score);
		}
		
		int[] answer = new int[ar.size()];
		
		for(int i=0;i<ar.size();i++) {
			answer[i] = ar.get(i).intValue();
		};
        
        return answer;
    }
}

 

반응형

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

숫자 짝꿍  (0) 2024.02.25
실패율  (0) 2024.02.23
푸드 파이트 대회  (0) 2024.02.21
2016년  (0) 2020.08.31
모의고사  (0) 2020.08.01
반응형

연습문제 유형으로 구현 방식으로 문제 접근하여 풀이

프로그래머스: 푸드 파이트 대회 (Lv.1)

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

 

프로그래머스

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

programmers.co.kr

 

class Solution {
    public String solution(int[] food) {
        String answer = "";
		int[] cnt = new int[food.length];
		
		for(int i=1;i<food.length;i++) {
			int foodCnt = food[i];
			
			if(food[i]%2 == 1) foodCnt = food[i] - 1; 
			cnt[i] = foodCnt / 2;
		}
		
		for(int i=1;i<cnt.length;i++) {
			for(int j=0;j<cnt[i];j++) {
				answer += "" + i;
			}
		}
		
		answer += "0";
		
		for(int i=cnt.length-1;i>0;i--) {
			for(int j=0;j<cnt[i];j++) {
				answer += "" + i;
			}
		}
        
        return answer;
    }
}

 

반응형

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

숫자 짝꿍  (0) 2024.02.25
실패율  (0) 2024.02.23
추억 점수  (0) 2024.02.22
2016년  (0) 2020.08.31
모의고사  (0) 2020.08.01
반응형

프로그래머스: 2016년 문제

https://programmers.co.kr/learn/courses/30/lessons/12901

 

풀이 방식:

입력받은 a월 b일과 1월 1일 금요일을 기준으로 날짜수의 차이를 구한 후, 7로 나눈 다음 나머지 값으로 days[] 배열에서 해당 요일 조회

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

class Solution {
    public String solution(int a, int b) {
        String answer = "";
        String date1 = "2016-01-01";
		String date2 = "2016-"+a+"-"+b;
		String[] days = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
		
		try {			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date firstDate = format.parse(date1);
			Date secondDate = format.parse(date2);
			
			long timeDiff = secondDate.getTime() - firstDate.getTime();
			long calTimeDiff = timeDiff / (24*60*60*1000);
			
			int remainder = (int)calTimeDiff % 7;
			
			answer = days[remainder];
			
		}catch(ParseException e){
			
		}
        
        return answer;
    }
}

 

[Github]

https://github.com/blackmount22/algorithm-programmers/blob/master/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4%20-%202016.txt

반응형

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

숫자 짝꿍  (0) 2024.02.25
실패율  (0) 2024.02.23
추억 점수  (0) 2024.02.22
푸드 파이트 대회  (0) 2024.02.21
모의고사  (0) 2020.08.01
반응형

완전 탐색(Brute-Force)

: 컴퓨터의 빠른 계산 능력을 이용하여 모든 경우의 수를 전부 탐색하는 방법으로, 탐색하는 방식에 따라

Brute-Force(for문, if문을 이용하여 처음부터 끝까지 탐색하는 방법), DFS/BFS 그래프 알고리즘, 백트래킹 등이 있다.

단점으로는 완전탐색이다 보니 답을 찾는데 시간이 오래 걸린다.

 

프로그래머스: 모의고사 문제

https://programmers.co.kr/learn/courses/30/lessons/42840

import java.util.ArrayList;
import java.util.Collections;

class Solution {
    public int[] solution(int[] answers) {
		int[] studentA = {1,2,3,4,5};
		int[] studentB = {2,1,2,3,2,4,2,5};
		int[] studentC = {3,3,1,1,2,2,4,4,5,5};
		int[] result;
		
		int answerA = 0;
		int answerB = 0;
		int answerC = 0;
		
		for(int i=0;i<answers.length;i++) {
			if(studentA[i%studentA.length] == answers[i]) answerA++;
			if(studentB[i%studentB.length] == answers[i]) answerB++;
			if(studentC[i%studentC.length] == answers[i]) answerC++;
		}
		
		int maxCnt = Math.max(Math.max(answerA, answerB), answerC);
		ArrayList<Integer> ar = new ArrayList<Integer>();
		
		if(maxCnt == answerA) {
			ar.add(1);
		}
		
		if(maxCnt == answerB) {
			ar.add(2);
		}
		
		if(maxCnt == answerC) {
			ar.add(3);
		}
		
		Collections.sort(ar);
		result = new int[ar.size()];
		for(int i=0;i < ar.size();i++) {
			result[i] = ar.get(i);
		}
        
        return result;
    }
}

 

반응형

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

숫자 짝꿍  (0) 2024.02.25
실패율  (0) 2024.02.23
추억 점수  (0) 2024.02.22
푸드 파이트 대회  (0) 2024.02.21
2016년  (0) 2020.08.31

+ Recent posts