[프로그래머스]더 맵게(JAVA)

JESS YANG·2021년 5월 2일
0

프로그래머스

목록 보기
8/13
post-thumbnail

문제

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

내 코드

public int solution(int[] scoville, int k) {
	int result = 0;
    	//1
	PriorityQueue<Integer> queue = new PriorityQueue();
	for(int sco : scoville) {
		queue.offer(sco);
	}
    //2
	while(queue.peek() < k) {
		//모든 음식의 스코빌 지수를 K 이상으로 만들 수 없는 경우에는 -1을 return 합니다.
		if(queue.size() == 1)
			return -1;
		result += 1;
		queue.offer(queue.poll() + queue.poll()*2);
	}
	
	return result;
}

풀이

특별히 어렵지 않게 풀었던 문제다.
문제에서 주어진 공식을 코드에 그대로 적용하면 쉽게 풀린다.

  1. 파라미터로 들어온 scoville array를 priotiryQueue로 정배열 시킨다.
  2. 모든 스코빌 지수가 K이상이 될때까지 반복문을 돌린다. 그리고 문제에서 주어진 스코빌 지수 공식을 적용한다.

0개의 댓글