Talentd
FleetCode

3610. Minimum Number of Primes to Sum to Target

Medium59.8% acceptance1 companyPremium elsewhere, free here

You are given two integers n and m.

You have to select a multiset of prime numbers from the first m prime numbers such that the sum of the selected primes is exactly n. You may use each prime number multiple times.

Return the minimum number of prime numbers needed to sum up to n, or -1 if it is not possible.

Example 1:

Input: n = 10, m = 2

Output: 4

Explanation:

The first 2 primes are [2, 3]. The sum 10 can be formed as 2 + 2 + 3 + 3, requiring 4 primes.

Example 2:

Input: n = 15, m = 5

Output: 3

Explanation:

The first 5 primes are [2, 3, 5, 7, 11]. The sum 15 can be formed as 5 + 5 + 5, requiring 3 primes.

Example 3:

Input: n = 7, m = 6

Output: 1

Explanation:

The first 6 primes are [2, 3, 5, 7, 11, 13]. The sum 7 can be formed directly by prime 7, requiring only 1 prime.

Constraints:

  • 1 <= n <= 1000
  • 1 <= m <= 1000

Asked at 1 company

Hints open one at a time

Hint 1
Can we use dynamic programming?
Hint 2
Generate the first m prime numbers.
Hint 3
Let dp[i] represent the minimum number of prime numbers that sum up to i. Use the generated prime numbers for the transition.
4:41
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
10
2
Input
15
5
Input
7
6
Your code is saved on this device.Solution