1. 문제
https://programmers.co.kr/learn/courses/30/lessons/42626?language=java
코딩테스트 연습 - 더 맵게
매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같��
programmers.co.kr
2. 나의 풀이
import java.util.PriorityQueue;
class Solution {
public int solution(int[] scoville, int K) {
int answer = 0;
//System.out.println(check(scoville, K));
PriorityQueue <Integer> q = new PriorityQueue();
for(int imsi : scoville) {
q.add(imsi);
}
try {
while(check(q, K)) {
q.add(q.poll() + (q.poll() * 2));
answer += 1;
}
}catch(Exception e){
answer = -1;
}
return answer;
}
boolean check(PriorityQueue <Integer> scoville, int K) {
boolean answer = false;
for(int imsi : scoville) {
if(imsi < K) {
answer = true;
break;
}
}
return answer;
}
}
3. 다른 사람 풀이
import java.util.*;
class Solution {
public int solution(int[] scoville, int K) {
PriorityQueue<Integer> q = new PriorityQueue<>();
for(int i = 0; i < scoville.length; i++)
q.add(scoville[i]);
int count = 0;
while(q.size() > 1 && q.peek() < K){
int weakHot = q.poll();
int secondWeakHot = q.poll();
int mixHot = weakHot + (secondWeakHot * 2);
q.add(mixHot);
count++;
}
if(q.size() <= 1 && q.peek() < K)
count = -1;
return count;
}
}
'알고리즘 공부 > 프로그래머스' 카테고리의 다른 글
(Java)프로그래머스 코딩테스트 연습 - 해시 - 전화번호 목록 (0) | 2020.10.21 |
---|---|
(Java)프로그래머스 코딩테스트 연습 - 완전탐색 - 소수 찾기 (0) | 2020.10.20 |
(Java)프로그래머스 코딩테스트 연습 - 월간 코드 챌린지 시즌1 - 삼각 달팽이 (0) | 2020.10.19 |
(Java)프로그래머스 코딩테스트 연습 - 2017 카카오코드 예선 - 카카오프렌즈 컬러링북 (0) | 2020.10.15 |
프로그래머스 코딩테스트 연습 - Summer/Winter Coding(~2018) - 스킬트리 (0) | 2020.10.13 |