LeetCode 풀기 - 1046. Last Stone Weight

백지우·2026년 9월 2일

LeetCode

목록 보기
23/76
  • LeetCode 최소 하루에 하나는 풀자는 마인드
  • 사용언어: JAVA

1046. Last Stone Weight

풀이

  • 방법: priority queue를 reverse order로 사용하여 해결
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        int m1;
        int m2;

        for(int stone: stones){
            pq.offer(stone);
        }

        while(pq.size() > 1){
            m1 = pq.poll();
            m2 = pq.poll();

            if(m1 > m2) pq.offer(m1-m2);
        }

        return (pq.isEmpty()) ? 0: pq.peek();
    }
}

0개의 댓글