LeetCode solutions

3723. Sum of Good Numbers

My accepted Python solution to LeetCode problem 3723, Sum of Good Numbers, running in 2ms.

  • Difficulty: Easy
  • Python
  • Runtime 2ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 2ms, memory 17.4MB, accepted 2025-12-31.

python
class Solution:
    def sumOfGoodNumbers(self, nums: List[int], k: int) -> int:
        n = len(nums)
        total = 0
        for i in range(n):
            is_good = True
            # Check i - k
            if i - k >= 0 and nums[i] <= nums[i - k]:
                is_good = False
            # Check i + k
            if i + k < n and nums[i] <= nums[i + k]:
                is_good = False
            if is_good:
                total += nums[i]
        return total

Source