LeetCode solutions

2648. Number of Ways to Earn Points

My accepted Python solution to LeetCode problem 2648, Number of Ways to Earn Points, running in 714ms.

  • Difficulty: Hard
  • Python
  • Runtime 714ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 714ms, memory 17.5MB, accepted 2025-12-31.

python
class Solution:
    def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:
        MOD = 10**9 + 7
        n = len(types)
        
        # dp[j] = number of ways to get exactly j points
        dp = [0] * (target + 1)
        dp[0] = 1
        
        for count, marks in types:
            # Process each question type
            # Need to iterate backwards to avoid using same type multiple times in one iteration
            new_dp = dp[:]
            for num_solved in range(1, count + 1):
                points = num_solved * marks
                if points > target:
                    break
                for j in range(points, target + 1):
                    new_dp[j] = (new_dp[j] + dp[j - points]) % MOD
            dp = new_dp
        
        return dp[target]

Source