-
프로그래머스 42626 더 맵게 (PriorityQueue)알고리즘 공부 2023. 5. 14. 17:15
https://school.programmers.co.kr/learn/courses/30/lessons/42626
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr

1. PriorityQueue 에 배열을 넣어서 첫번째, 두번째 값을 빼고 스코빌 지수 계산식에 다시 넣어주는 형식
- 배열이 기본적으로 오른차순이 되기 때문에 첫번째 값이 K 보다 높으면 answer 리턴
- 스코빌 지수를 K 이상으로 만들 수 없는 경우 : 큐에 값이 하나 남았는데 K 미만일때 (-1 리턴)
import java.util.*; class Solution { public int solution(int[] scoville, int K) { int answer = 0; PriorityQueue<Integer> q = new PriorityQueue<>(); for(int s : scoville){ q.offer(s); } if(q.peek()>=K) return 0; int a = q.size(); while(q.peek()<K) { int f1 = q.poll(); int f2 = q.poll(); q.offer(f1+(f2*2)); answer++; if(q.size()==1 && q.peek()<K) return -1; } return answer; } }'알고리즘 공부' 카테고리의 다른 글
프로그래머스 12913 땅따먹기 (DP) (0) 2023.05.14 프로그래머스 12900 2xn 타일링 (피보나치 수열) (0) 2023.05.14 프로그래머스 42587 프로세스 (queue, priorityQueue) (0) 2023.05.13 프로그래머스 42746 가장 큰 수 (배열의 Sort 재정의) (0) 2023.05.13 프로그래머스 154539 뒤에 있는 큰 찾기 (0) 2023.05.13