From ae8edaea7303b5665c0e36ab25581c23050fe836 Mon Sep 17 00:00:00 2001 From: Ayush-Jain <68059717+whycodebro@users.noreply.github.com> Date: Thu, 27 Oct 2022 12:54:14 +0530 Subject: [PATCH] Create Last Stone Weight.java --- Leetcode/Last Stone Weight.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Leetcode/Last Stone Weight.java diff --git a/Leetcode/Last Stone Weight.java b/Leetcode/Last Stone Weight.java new file mode 100644 index 00000000..9e3ff570 --- /dev/null +++ b/Leetcode/Last Stone Weight.java @@ -0,0 +1,33 @@ +class Solution { + public int lastStoneWeight(int[] stones) { + + PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); + + for(int i : stones){ + pq.add(i); + } + + Iterator it = pq.iterator(); + + while(it.hasNext()){ + + if(pq.size() == 1){ + return pq.peek(); + } + + int a = pq.poll(); + int b = pq.poll(); + + + + if(a>b){ + a = a-b; + pq.add(a); + }else{ + b = b-a; + pq.add(b); + } + } + return 0; + } +}