Skip to content
DSA Patterns
Menu
Language
Knapsack & Subset DPLC #8795 of 6← PreviousNext →

Demo preview: These solutions pass the automated test suite but have not been human-reviewed. Treat trade-offs and prose as draft.

Profitable Schemes

Problem (restated)

G crimes with group[i] members and profit[i]. At most n members total. Count schemes with profit ≥ minProfit (mod 10^9+7). Empty scheme has profit 0.

Intuition

0/1 knapsack: capacity members and capped profit (minProfit is enough). Count ways.

Approaches

2D knapsack (members × profit)

Unverified
Time O(G·n·P)Space O(n·P)

Idea. dp[i][j] ways with i members and profit min(j, minProfit). Reverse iterate per crime. Sum dp[*][minProfit].

Walkthrough. n=5, minProfit=3, group=[2,2], profit=[2,3] → 2.

Trade-offs. Cap profit dimension at minProfit to keep state small.

Solution
export function profitableSchemes(
  n: number,
  minProfit: number,
  group: number[],
  profit: number[],
): number {
  const MOD = 1_000_000_007;
  const dp = Array.from({ length: n + 1 }, () => Array(minProfit + 1).fill(0));
  dp[0]![0] = 1;
  for (let k = 0; k < group.length; k++) {
    const members = group[k]!, p = profit[k]!;
    for (let i = n; i >= members; i--) {
      for (let j = minProfit; j >= 0; j--) {
        const nj = Math.min(minProfit, j + p);
        dp[i]![nj] = (dp[i]![nj]! + dp[i - members]![j]!) % MOD;
      }
    }
  }
  let ans = 0;
  for (let i = 0; i <= n; i++) ans = (ans + dp[i]![minProfit]!) % MOD;
  return ans;
}
export function profitableSchemes(
  n: number,
  minProfit: number,
  group: number[],
  profit: number[],
): number {
  const MOD = 1_000_000_007;
  const dp = Array.from({ length: n + 1 }, () => Array(minProfit + 1).fill(0));
  dp[0]![0] = 1;
  for (let k = 0; k < group.length; k++) {
    const members = group[k]!, p = profit[k]!;
    for (let i = n; i >= members; i--) {
      for (let j = minProfit; j >= 0; j--) {
        const nj = Math.min(minProfit, j + p);
        dp[i]![nj] = (dp[i]![nj]! + dp[i - members]![j]!) % MOD;
      }
    }
  }
  let ans = 0;
  for (let i = 0; i <= n; i++) ans = (ans + dp[i]![minProfit]!) % MOD;
  return ans;
}

Template connection

dp[members][profit] counts schemes. Each crime walks both dimensions downward. Profit past minProfit lands in the minProfit bucket. The answer adds every member count in that bucket, modulo 10^9+7.

Reflection

  • dp[k][p] counts schemes with at most k members and profit capped at minProfit. Each group is used once, so both loops walk backward.
  • Reduce modulo 10**9 + 7. A group with more members than n never enters the loop. Profit is capped at minProfit, not rejected.
  • No groups: doing nothing is 1 scheme when minProfit is 0, and 0 schemes otherwise. The code sums dp[i][minProfit].
Contributor review checklist

Interactive

Mental model

A worked animation for this problem. Scrub steps or press space to pause; re-tell the invariant out loud.

Step 1 of 6
1/6

Profitable schemes: n=5 members, minProfit=3, two crimes (2 members/profit 2) and (2/3).

A:2/2
0
B:2/3
1

empty profit = 0

Key:CurrentMatchSettled