LeetCode solutions

2067. Maximum Number of Points with Cost

My accepted Python solution to LeetCode problem 2067, Maximum Number of Points with Cost, running in 391ms.

  • Difficulty: Medium
  • Python
  • Runtime 391ms
  • Memory 40.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 391ms, memory 40.2MB, accepted 2025-12-29.

python
class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        m, n = len(points), len(points[0])
        dp = points[0][:]
        
        for i in range(1, m):
            # Left to right max
            left = [0] * n
            left[0] = dp[0]
            for j in range(1, n):
                left[j] = max(left[j-1] - 1, dp[j])
            
            # Right to left max
            right = [0] * n
            right[n-1] = dp[n-1]
            for j in range(n-2, -1, -1):
                right[j] = max(right[j+1] - 1, dp[j])
            
            # Update dp
            for j in range(n):
                dp[j] = points[i][j] + max(left[j], right[j])
        
        return max(dp)

Source