Profitable Schemes solution explained

Profitable Schemes is a counting knapsack with two dimensions: people used and profit earned. The trick is to cap the profit dimension at the target, because once a plan reaches the target any extra profit makes no difference. Keep ways[p][g], the number of job sets using exactly p people with capped profit g, add each job by sweeping p downward, and sum ways[p][target] over every p. That is O(J * P * T) time for J jobs, P people and target T, with the count reported modulo 1,000,000,007.

By Published 7 min read

The problem in plain words

A team has P people. There is a list of jobs; job i needs staff[i] people (at least one, in the usual version) and earns gain[i] (zero or more). Nobody can work two jobs, so a plan is a set of jobs whose staff needs add up to P or less. A plan is profitable if its gains add up to at least T. Count the profitable plans. Two plans are different when they contain a different set of jobs, even if the totals match. The count can be enormous, so the answer is reported modulo 1,000,000,007.

Hand-made inputs, answers checked against full enumeration
P, TJobs as (staff, gain)AnswerWhy
5, 3(2, 2), (3, 1), (1, 2)3First + third, second + third, first + second
4, 0(1, 0), (2, 0)4Every set fits, including taking no jobs
1, 5(1, 9)1The single job is enough on its own
3, 10(1, 2), (1, 3)0Even both jobs earn only 5
6, 4(2, 3), (2, 1), (3, 2), (1, 2)7Seven of the 16 sets qualify
Hand-made inputs, answers checked against full enumeration. Scroll sideways on a small screen.

What the interviewer is checking

This is a harder knapsack variant, and the interviewer is usually looking for two specific ideas. The first is recognising a counting knapsack: each job is taken or skipped, the team size is a capacity, and you sum ways instead of maximising a value. The second is the cap. Profit could in principle reach the sum of all gains, and a table that wide is wasteful. Noticing that 'at least T' only needs to know whether you got to T, and therefore storing min(profit, T), is the insight that makes the table small. Saying clearly what a cell means (exactly p people, or at most p) is the third thing they listen for, because off-by-one mistakes here are common.

Clarifying questions

  • Does the empty plan count? It does when T is 0, since it uses nobody and earns 0, which is at least 0.
  • Can a job need zero people? Ask. With staff of at least one the downward sweep is clearly safe; the table here also handles zero correctly, but it is worth confirming.
  • Is it the number of plans or the best profit? Counting plans is the usual version. The best-profit version would be a normal knapsack.

Brute force: every set of jobs

Loop over every bitmask from 0 to 2^J - 1, add up the staff and gain of the chosen jobs, and count the masks that fit and earn enough. It is a few lines, it is exactly the definition, and it is the reference used to test the table. At 20 jobs it checks about a million sets; at 40 it would need about a trillion.

@@profitable-schemes/slow.py@@
Python: enumerate all job sets (reference only).

The pattern: counting knapsack with a capped threshold

Define ways[p][g] as the number of job sets considered so far that use exactly p people and have profit g, where g means min(actual profit, T). Before any job, only the empty set exists: ways[0][0] = 1. Adding a job with need people and earn profit means every existing set either skips it (the table already counts those) or takes it, moving from cell (p - need, g) to cell (p, min(T, g + earn)). Sweep p from P down to need so the cell you read was not already updated for this job. The answer is the sum of ways[p][T] for p from 0 to P.

@@profitable-schemes/fast.py@@
Python: the capped table, with the remainder taken after each addition.

Many published solutions use 'at most p people' instead: start every ways[p][0] at 1 and return ways[P][T] directly. Both are correct. The 'exactly' form is easier to explain because each cell counts a disjoint group of plans, and the final sum makes the 'at most P people' rule visible.

In JavaScript, every cell stays below 1,000,000,007, so adding two cells never exceeds about 2.1 billion. That is well inside the 2^53 range where numbers are exact, and no BigInt is needed. Multiplying two counts would be a different story, but this problem only adds.

@@profitable-schemes/fast.js@@
JavaScript: the same table.

Tracing the first example

Team of 5, target 3, jobs (2, 2), (3, 1), (1, 2). After the first job, ways[0][0] = 1 (nothing taken) and ways[2][2] = 1. The second job, needing 3 people, adds ways[3][1] = 1 from the empty set and ways[5][3] = 1 from the first job, because 2 + 1 reaches the target. The third job, needing 1, adds four sets: alone to ways[1][2], with the first job to ways[3][3] (profit 4 capped at 3), with the second to ways[4][3], and with both to cell 6, which does not exist because the team has only 5 people. Column 3 now holds ones at p = 3, 4 and 5, and the answer is 3.

Complexity

J jobs, team size P, profit target T
ApproachTimeExtra memory
Enumerate every job setO(2^J * J)O(1)
Table without the capO(J * P * total gain)O(P * total gain)
Table with profit capped at TO(J * P * T)O(P * T)
Memoized recursion on (job, people left, profit still needed)O(J * P * T)O(J * P * T) plus the call stack
J jobs, team size P, profit target T. Scroll sideways on a small screen.

Edge cases

  • T = 0: every plan that fits counts, including the empty one.
  • A job that needs more people than the team has: it can never be taken, and the downward loop simply skips it.
  • A job with zero gain: it still changes the count, because adding it to a profitable plan makes another profitable plan.
  • Team size 0: only the empty plan fits, so the answer is 1 if T is 0 and 0 otherwise.
  • Large inputs where the count overflows 32 bits long before the final remainder.

Mistakes that sink this problem

  • Sizing the profit dimension to the total of all gains. It works but wastes time and memory, and interviewers notice the missed cap.
  • Writing to dst[g + earn] without the min, which indexes past the end of the row.
  • Sweeping p upward, which lets one job join the same plan more than once.
  • Returning only ways[P][T] from the 'exactly p people' table, which drops every plan that leaves people idle.
  • Taking the remainder only at the end in a language with fixed-size integers.

How to explain it out loud

Fictional example

Getting to the cap
Candidate
Each job is take or skip, people are a capacity, and we're counting sets, so it's a counting knapsack.
Candidate
Profit is the awkward dimension, because it could get huge. But the question only cares whether we reach the target, so I'll store profit as the smaller of the real total and the target. That makes the table people by target plus one.
Interviewer
What does a cell mean exactly?
Candidate
Number of job sets using exactly p people with capped profit g. I'll add jobs one at a time, sweep people downward, and at the end add up the target column across all p.

Ones and Zeroes uses the same two-dimensional 0/1 table but maximises a count instead of counting sets. Number of Dice Rolls With Target Sum is another counting table reported with a large remainder. Last Stone Weight II is a yes-or-no subset question with one dimension. Target Sum and Coin Change II are good next steps for counting knapsacks.

Rehearsing it with Natively

Profitable Schemes is a good problem to rehearse with someone asking 'what does a cell mean?' at the worst moment. During practice, Natively on Mac or Windows can read the problem from a screenshot, offer the capped-profit idea when you are stuck on the table size, and question your loop direction the way an interviewer would. The transcript it keeps afterwards makes it easy to see where your explanation got vague.

Questions people ask

How do you solve Profitable Schemes?

Use a counting knapsack. Keep ways[p][g], the number of job sets using exactly p people with profit min(total, target) equal to g. Add each job by sweeping p downward, then sum ways[p][target] over all p, modulo 1,000,000,007.

Why is profit capped at the target in Profitable Schemes?

The question only asks whether a plan reaches the target, so any profit above it behaves the same. Storing min(profit, target) shrinks the profit dimension to target + 1 columns.

What is the time complexity of Profitable Schemes?

O(J * P * T) time and O(P * T) memory for J jobs, team size P and profit target T.

Does the empty plan count in Profitable Schemes?

Only when the target is 0. Taking no jobs uses nobody and earns 0, which meets a target of 0 but no higher target.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP