Prime Subset Selection II Problem
Description
LeetCode Problem 4081.
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 <= 10001 <= s <= 1000
Solution
Approach: 0/1 knapsack that stores the best subset
There are at most 168 primes up to 1000 and the sum is at most 1000, so a knapsack over the sum is cheap. Let best[t] be the preferred subset with sum exactly t, where "preferred" means fewest primes first, then lexicographically smallest as an increasing array.
Process the primes from the largest to the smallest. A new prime p is smaller than every prime chosen so far, so adding it to best[t - p] puts p at the front of the array. Then compare this candidate with the current best[t] by (length, array) and keep the better one. Taking p first is safe for the tie-break: two candidates that start with the same p are compared by the rest, which are themselves the preferred subsets for the smaller sum. Loop t downward so that each prime is used at most once.
At the end, the answer is best[t] for the largest t <= s that is reachable and positive, or an empty array if there is none.
Complexity: about O(pi(n) * s) states, each handled by comparing short arrays; space O(s) arrays.
class Solution:
def maxPrimeSubset(self, n: int, s: int) -> List[int]:
primes = [p for p in range(2, n + 1) if all(p % d for d in range(2, int(p ** 0.5) + 1))]
# best[t]: the preferred subset with sum exactly t (fewest primes, then lexicographically smallest)
best = [None] * (s + 1)
best[0] = ()
# Going from the largest prime down, a new prime is smaller than everything already chosen,
# so it goes to the front of the increasing array.
for p in reversed(primes):
for t in range(s, p - 1, -1):
if best[t - p] is None:
continue
cand = (p,) + best[t - p]
cur = best[t]
if cur is None or (len(cand), cand) < (len(cur), cur):
best[t] = cand
for t in range(s, 0, -1):
if best[t] is not None:
return list(best[t])
return []