> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Profitable Schemes Python Solution with Tests

> Tested Python solution for LeetCode 879 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 879, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming), Knapsack Problem, 0-1 Knapsack. [View on LeetCode](https://leetcode.com/problems/profitable-schemes/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 879   # by problem number
lcpy gen -s profitable_schemes   # by problem name
```

## Problem

There is a group of \<code>n\</code> members, and a list of various crimes they could commit. The \<code>ith\</code> crime generates a \<code>profit\[i]\</code> and requires \<code>group\[i]\</code> members to participate in it. If a member participates in one crime, that member can't participate in another crime.\</p>

\<p>Let's call a \<em>profitable scheme\</em> any subset of these crimes that generates at least \<code>minProfit\</code> profit, and the total number of members participating in that subset of crimes is at most \<code>n\</code>.\</p>

\<p>Return the number of schemes that can be chosen. Since the answer may be very large, return it \<strong>modulo\</strong> \<code>10\<sup>9\</sup> + 7\</code>.

### Examples

```
Input: n = 5, minProfit = 3, group = [2,2], profit = [2,3]
Output: 2
Explanation: To make a profit of at least 3, the group could either commit crimes 0 and 1, or just crime 1.
In total, there are 2 schemes.
```

```
Input: n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation: Every subset of the crimes has total members at most 10 and profit at least 5,
and 7 subsets exist, so all of them are profitable schemes.
```

### Constraints

* 1 \<= n \<= 100
* 0 \<= minProfit \<= 100
* 1 \<= group.length \<= 100
* 1 \<= group\[i] \<= 100
* profit.length == group.length
* 0 \<= profit\[i] \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/profitable_schemes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/profitable_schemes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(len(group) * n * min_profit)
    # Space: O(n * min_profit)
    def profitable_schemes(
        self, n: int, min_profit: int, group: list[int], profit: list[int]
    ) -> int:
        mod = 1_000_000_007
        dp = [[0] * (min_profit + 1) for _ in range(n + 1)]
        dp[0][0] = 1
        for members, gain in zip(group, profit, strict=True):
            for used in range(n, members - 1, -1):
                for earned in range(min_profit, -1, -1):
                    new_earned = min(min_profit, earned + gain)
                    dp[used][new_earned] = (dp[used][new_earned] + dp[used - members][earned]) % mod
        return sum(dp[used][min_profit] for used in range(n + 1)) % mod
```

## Complexity

| Time | Space |
| - | - |
| O(len(group) \* n \* min\_profit) | O(n \* min\_profit) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.