[ Python ] ✅✅ Simple Python Solution Using Three Approach 🥳✌👍
If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Apporach 1 Using Priority Queue :-\n# Runtime: 39 ms, faster th
# If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Apporach 1 Using Priority Queue :-\n# Runtime: 39 ms, faster than 55.89% of Python3 online submissions for Last Stone Weight.\n# Memory Usage: 14.1 MB, less than 13.28% of Python3 online submissions for Last Stone Weight.\n\n\tfrom queue import PriorityQueue\n\n\tclass Solution:\n\t\tdef lastStoneWeight(self, stones: List[int]) -> int:\n\n\t\t\tpq = PriorityQueue()\n\n\t\t\tfor stone in stones:\n\t\t\t\tpq.put(-stone)\n\n\t\t\twhile pq.qsize() >= 2:\n\t\t\t\tfirst_heavy = -(pq.get())\n\t\t\t\tsecond_heavy = -(pq.get())\n\n\t\t\t\tif first_heavy == second_heavy:\n\t\t\t\t\tcontinue\n\t\t\t\telse:\n\t\t\t\t\tpq.put(-(first_heavy - second_heavy))\n\n\t\t\tif pq.qsize() == 1:\n\t\t\t\treturn -pq.get()\n\t\t\telse:\n\t\t\t\treturn 0\n\n# Approach 2 Using Sorting :-\n\n\tclass Solution:\n\t\tdef lastStoneWeight(self, stones: List[int]) -> int:\n\n\t\t\twhile len(stones)>1:\n\n\t\t\t\tsort_stones = sorted(stones)\n\n\t\t\t\tsmall_num, large_num = sort_stones[-2], sort_stones[-1]\n\n\t\t\t\tif small_num == large_num :\n\n\t\t\t\t\tstones = sort_stones[:-2]\n\t\t\t\telse:\n\n\t\t\t\t\tsort_stones.remove(small_num)\n\n\t\t\t\t\tsort_stones.remove(large_num)\n\n\t\t\t\t\tsort_stones.append(large_num - small_num)\n\n\t\t\t\t\tstones = sort_stones\n\n\t\t\tif len(stones)==1:\n\n\t\t\t\treturn stones[0]\n\n\t\t\telse:\n\n\t\t\t\treturn 0\n\n# Approach 3 Using SortedList:\n# Runtime: 47 ms, faster than 7.84% of Python3 online submissions for Last Stone Weight.\n# Memory Usage: 14.2 MB, less than 8.03% of Python3 online submissions for Last Stone Weight.\nfrom sortedcontainers import SortedList\n\n\tclass Solution:\n\t\tdef lastStoneWeight(self, stones: List[int]) -> int:\n\n\t\t\tstones = SortedList(stones)\n\n\t\t\twhile len(stones) > 1:\n\n\t\t\t\tlast = stones.pop(-1)\n\t\t\t\tsecond_last = stones.pop(-1)\n\n\t\t\t\tif last == second_last:\n\t\t\t\t\tcontinue\n\t\t\t\telse:\n\t\t\t\t\tstones.add(last - second_last)\n\n\t\t\tif len(stones) > 0:\n\t\t\t\treturn stones[0]\n\t\t\telse:\n\t\t\t\treturn 0\n\n# Thank You \uD83E\uDD73\u270C\uD83D\uDC4D\n
['Sorting', 'Heap (Priority Queue)', 'Python', 'Python3']