LeetCode solutions

2695. Find Score of an Array After Marking All Elements

My accepted Python solution to LeetCode problem 2695, Find Score of an Array After Marking All Elements, running in 486ms.

  • Difficulty: Medium
  • Python
  • Runtime 486ms
  • Memory 38.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 486ms, memory 38.9MB, accepted 2025-12-31.

python
class Solution:
    def findScore(self, nums: List[int]) -> int:
        import heapq
        
        n = len(nums)
        marked = [False] * n
        
        # Min heap: (value, index)
        heap = [(nums[i], i) for i in range(n)]
        heapq.heapify(heap)
        
        score = 0
        
        while heap:
            val, idx = heapq.heappop(heap)
            
            if marked[idx]:
                continue
            
            score += val
            marked[idx] = True
            
            # Mark adjacent elements
            if idx > 0:
                marked[idx - 1] = True
            if idx < n - 1:
                marked[idx + 1] = True
        
        return score

Source