Skip to main content

2 posts tagged with "Array"

View All Tags

import heapq


class Solution:
def lastStoneWeight(self, stones: list[int]) -> int:
stones.append(0)
stones = list(map(lambda x: -x, stones))
heapq.heapify(stones)
while len(stones) > 1:
y, x = -heapq.heappop(stones), -heapq.heappop(stones)
if x != y:
heapq.heappush(stones, -(y - x))
return -heapq.heappop(stones)

class Solution1:
def removeDuplicates(self, nums: list[int]) -> int:
v, p = -101, 0 # ng
for i in range(len(nums)):
if nums[i] > v:
v = nums[p] = nums[i]
p += 1
return p


class Solution:
def removeDuplicates(self, nums: list[int]) -> int:
p = 1
for i in range(1, len(nums)):
if nums[p - 1] < nums[i]:
nums[p], p = nums[i], p + 1
return p