2Brute2Best
🔥0

Profitable Schemes

Hard·LeetCode #879 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

A gang has n members and a list of crimes. Crime i needs group[i] members and earns profit[i]. A member who joins one crime cannot join another.

A scheme is any set of crimes where the members needed, added up, are at most n, and the profit, added up, is at least minProfit. Count the schemes. The empty set of crimes is a scheme when minProfit is 0.

Return the count modulo 10^9 + 7.

Input

n, the number of members. minProfit, the profit goal. group, the members each crime needs. profit, the profit of each crime (the same length as group).

Output

The number of schemes with at least minProfit profit and at most n members in total, modulo 10^9 + 7.

Example 1
Input: n = 5, minProfit = 3, group = [2, 2], profit = [2, 3]
Output: 2

Crime 1 alone earns 3, which is enough. Crimes 0 and 1 together earn 5 and use 4 members, which fits in 5. Crime 0 alone earns only 2, so it does not count.

Example 2
Input: n = 10, minProfit = 5, group = [2, 3, 5], profit = [6, 7, 8]
Output: 7

Every crime alone already earns at least 5, and all three together use exactly 10 members. So every set with at least one crime counts: 2³ − 1 = 7 schemes.

Constraints
  • • 1 ≤ n ≤ 100
  • • 0 ≤ minProfit ≤ 100
  • • 1 ≤ group.length ≤ 100
  • • 1 ≤ group[i] ≤ 100
  • • profit.length == group.length
  • • 0 ≤ profit[i] ≤ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“a group of n members”
→ The members used are a capacity limit, like the weight limit in Knapsack. The total must stay at most n.
“each member joins at most one crime”
→ Each crime is used at most once, so the members loop goes from high to low.
“profit of at least minProfit”
→ Profit only needs to reach a goal. Cap it at minProfit, so no profit above the goal is lost.
“modulo 10^9 + 7”
→ The count can be huge, so every addition is taken modulo 10^9 + 7.
03

Approach Complexity Ladder

Brute Force — skip or do every crimeO(2^C)O(C)

Recurse over the crimes: skip each one, or do it if its members fit; count the finished schemes that earn enough

Better — memo on (crime, members used, capped profit)O(C · n · P)O(C · n · P)

The same recursion, with profit capped at minProfit and each (crime, members, profit) state solved once

Optimal — one 2D table over members and capped profitO(C · n · P)O(n · P)

dp[m][p] counts the schemes using at most m members with capped profit p; for each crime, members go from high to low

Brute Force Intuition

Walk the crimes in order. For each crime, either skip it, or do it when its members still fit. At the end, count the chosen set if its profit reaches minProfit. Every subset of crimes is one path, so every scheme is counted once.

Recognition rule for this sub-pattern:

Same table, but add counts instead of taking a max.
Remember this · DP — Knapsack
Trigger
Pick a subset to hit a target sum / capacity, partition into two groups, count the ways to reach an amount.
Think
Is this "choose some items so their total is X"?
Key question
Can each item be used once (loop capacity backwards) or many times (loop forwards)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Knapsack translate into production architectures and low-level memory mechanics.