inblog logo
|
LHS's Study Space
    알고리즘문제풀기프로그래머스

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

    lhs's avatar
    lhs
    Nov 22, 2024
    [프로그래머스] 더 맵게(42626)
    Contents
    1. 문제 풀이 아이디어2. 나의 정답 코드3. 정리
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/42626
     

    1. 문제 풀이 아이디어

    • PriorityQueue를 사용하면 문제를 해결할 수 있다.

    2. 나의 정답 코드

    class Solution { public int solution(int[] scoville, int K) { int answer = 0; PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); for (int i : scoville) { priorityQueue.add(i); } while (priorityQueue.peek() < K) { if (priorityQueue.size() == 1) { return -1; } int a = priorityQueue.poll(); int b = priorityQueue.poll(); priorityQueue.add(a + b * 2); answer++; } return answer; } }

    3. 정리

    • scoville 배열의 모든 값을 priorityQueue에 추가한다.
    • priorityQueue의 최솟값이 K보다 작을 때까지 반복한다.
    • priorityQueue의 크기가 1이면, 목표 스코빌 지수를 만들 수 없으므로 1을 반환한다.
    • 그렇지 않다면, 가장 작은 두 값을 꺼내 계산한 후 다시 priorityQueue에 넣고, 횟수를 증가한다.
    Share article

    LHS's Study Space

    RSS·Powered by Inblog