LeetCode879. Profitable Schemes——动态规划

文章目录

一、题目

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

Let’s call a profitable scheme any subset of these crimes that generates at least minProfit profit, and the total number of members participating in that subset of crimes is at most n.

Return the number of schemes that can be chosen. Since the answer may be very large, return it modulo 109 + 7.

Example 1:

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.
Example 2:

Input: n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation: To make a profit of at least 5, the group could commit any crimes, as long as they commit one.
There are 7 possible schemes: (0), (1), (2), (0,1), (0,2), (1,2), and (0,1,2).

Constraints:

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

二、题解

class Solution {
   
public:
    int profitableSchemes(int n, int minProfit, vector<int>& group, vector<int>& profit) {
   
        int mod = 1e9 + 7;
        vector<vector<int>> dp(n+1,vector<int>(minProfit+1,0));
        for(int r = 0;r <= n;r++){
   
            dp[r][0] = 1;
        }
        int m = group.size();
        for(int i = m - 1;i >= 0;i--){
   
            for(int r = n;r >= 0;r--){
   
                for(int s = minProfit;s >= 0;s--){
   
                    int p1 = dp[r][s];
                    int p2 = group[i] <= r ? dp[r-group[i]][max(0,s-profit[i])] : 0;
                    dp[r][s] = (p1 + p2) % mod;
                }
            }
        }
        return dp[n][minProfit] % mod;
    }
};

相关推荐

  1. LeetCode879. Profitable Schemes——动态规划

    2024-02-15 14:40:01       51 阅读
  2. LeetCode——动态规划

    2024-02-15 14:40:01       42 阅读
  3. [LeetCode]-动态规划-4

    2024-02-15 14:40:01       46 阅读
  4. leetcode动态规划专题

    2024-02-15 14:40:01       43 阅读
  5. LeetCode动态规划--题目练习

    2024-02-15 14:40:01       41 阅读
  6. leetcode-动态规划-01背包

    2024-02-15 14:40:01       28 阅读
  7. [leetcode,动态规划] 完全平方数

    2024-02-15 14:40:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-15 14:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-15 14:40:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-15 14:40:01       82 阅读
  4. Python语言-面向对象

    2024-02-15 14:40:01       91 阅读

热门阅读

  1. [缓存] - 3.金融交易系统缓存架构设计

    2024-02-15 14:40:01       50 阅读
  2. 【for循环——讲解】

    2024-02-15 14:40:01       44 阅读
  3. LED照明

    2024-02-15 14:40:01       48 阅读
  4. CCF-CSP 202206-2 寻宝!大冒险!

    2024-02-15 14:40:01       52 阅读
  5. 网络安全产品之认识蜜罐

    2024-02-15 14:40:01       63 阅读
  6. 七种SQL进阶用法

    2024-02-15 14:40:01       47 阅读
  7. conda env退回到之前的版本

    2024-02-15 14:40:01       42 阅读
  8. ubuntu下conda如何设置镜像源(清华镜像源)

    2024-02-15 14:40:01       54 阅读
  9. IDEA玩转Git

    2024-02-15 14:40:01       60 阅读
  10. 12.13 校招 实习 内推 面经

    2024-02-15 14:40:01       57 阅读
  11. Spring Cloud Eureka:服务注册与发现

    2024-02-15 14:40:01       48 阅读