All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1001_1100.s1046_last_stone_weight.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g1001_1100.s1046_last_stone_weight;

// #Easy #Array #Heap_Priority_Queue #Level_1_Day_15_Heap
// #2022_02_27_Time_2_ms_(73.81%)_Space_42.3_MB_(5.13%)

import java.util.PriorityQueue;

public class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue heap = new PriorityQueue<>((a, b) -> b - a);
        for (int stone : stones) {
            heap.offer(stone);
        }
        while (!heap.isEmpty()) {
            if (heap.size() >= 2) {
                int one = heap.poll();
                int two = heap.poll();
                int diff = one - two;
                heap.offer(diff);
            } else {
                return heap.poll();
            }
        }
        return -1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy