Talentd
FleetCode

4081. Prime Subset Selection II

Hard40.9% acceptance

You are given two integers n and s.

Consider all prime numbers less than or equal to n. Select a subset of these primes such that:

  • Each prime can be selected at most once.
  • The sum of the selected primes is as large as possible while remaining less than or equal to s.
  • Among subsets with the maximum sum, select one containing the fewest primes.
  • If multiple subsets remain tied, select the one whose elements, written in increasing order, form the lexicographically smallest array.

Return an integer array containing the selected primes in increasing order. If no valid non-empty subset exists, return an empty array.

Example 1:

Input: n = 7, s = 20

Output: [2, 3, 5, 7]

Explanation:

  • The prime numbers less than or equal to n = 7 are 2, 3, 5, and 7.
  • The sum of all these primes is 2 + 3 + 5 + 7 = 17, which is less than or equal to s = 20.
  • Thus, the answer is [2, 3, 5, 7].

Example 2:

Input: n = 15, s = 18

Output: [5,13]

Explanation:

  • The prime numbers less than or equal to 15 are 2, 3, 5, 7, 11, and 13.
  • The subsets with sum 18 are [5, 13], [7, 11], [2, 3, 13], and [2, 5, 11]. Since this sum equals s, it is the maximum possible sum.
  • Among these subsets, [5, 13] and [7, 11] contain the fewest primes: two.
  • Between these two subsets, [5, 13] is lexicographically smaller because its first element, 5, is smaller than 7. Thus, the answer is [5, 13].

Example 3:

Input: n = 5, s = 1

Output: []

Explanation:

  • The smallest prime number is 2, which is greater than s = 1.
  • Thus, no prime can be selected, so the answer is an empty array.

Constraints:

  • 1 <= n <= 1000
  • 1 <= s <= 1000

Hints open one at a time

Hint 1
1a (Minimum-Count DP): Generate the primes in increasing order. Let dp[i][t] be the minimum number of primes needed to obtain sum t using only primes from index i onward.
Hint 2
1b (Minimum-Count DP): At each prime, either skip it or select it once. Find the largest reachable sum at most s; its DP value gives the minimum number of selected primes.
Hint 3
1c (Minimum-Count DP): Reconstruct by scanning primes in increasing order. Select the current prime whenever the remaining suffix can complete the required sum with exactly one fewer prime. This gives the lexicographically smallest optimal array.
Hint 4
2a (Bitset DP): For each number of selected primes, maintain a bitset whose set bits represent achievable sums. Selecting a prime shifts the bitset for one fewer selected prime by that prime's value.
Hint 5
2b (Bitset DP): Build these states for suffixes of the sorted prime list, discarding sums above s. Find the largest reachable sum, then the smallest number of primes that achieves it.
Hint 6
2c (Bitset DP): Reconstruct in increasing prime order, selecting a prime whenever the suffix bitsets confirm that the remaining sum and count are achievable. The maximum count worth tracking is bounded by how many smallest primes fit within s.
Loading editor…
1class Solution:
2 def maxPrimeSubset(self, n: int, s: int) -> list[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
7
20
Input
15
18
Input
5
1
Your code is saved on this device.