There are G people in a gang, and a list of various crimes they could commit.

The i-th crime generates a profit[i] and requires group[i] gang members to participate.

If a gang 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 P profit, and the total number of gang members participating in that subset of crimes is at most G.

How many schemes can be chosen?  Since the answer may be very large, return it modulo 10^9 + 7.

Example 1:

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

Example 2:

Input: G = 10, P = 5, group = [2,3,5], profit = [6,7,8]
Output: 7
Explanation:
To make a profit of at least 5, the gang 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).

Note:

  1. 1 <= G <= 100
  2. 0 <= P <= 100
  3. 1 <= group[i] <= 100
  4. 0 <= profit[i] <= 100
  5. 1 <= group.length = profit.length <= 100

Approach #1: DP. [C++]

class Solution {
public:
int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
const int mod = 1000000007;
int K = group.size();
vector<vector<vector<int>>> dp(K+1, vector<vector<int>>(P+1, vector<int>(G+1, 0)));
dp[0][0][0] = 1;
for (int k = 1; k <= K; ++k) {
int p = profit[k-1];
int g = group[k-1];
for (int i = 0; i <= P; ++i) {
for (int j = 0; j <= G; ++j) {
dp[k][i][j] = (dp[k-1][i][j] + (j < g ? 0 : dp[k-1][max(0, i-p)][j-g])) % mod;
}
}
} return accumulate(begin(dp[K][P]), end(dp[K][P]), 0LL) % mod;
}
};

  

Approach #2: DP. [Java]

class Solution {
private int mod = (int)1e9 + 7;
public int profitableSchemes(int G, int P, int[] group, int[] profit) {
int[][] dp = new int[G+1][P+1];
dp[0][0] = 1;
for (int k = 1; k <= group.length; ++k) {
int g = group[k-1];
int p = profit[k-1];
for (int i = G; i >= g; --i) {
for (int j = P; j >= 0; --j) {
dp[i][j] = (dp[i][j] + dp[i-g][Math.max(0, j-p)]) % mod;
}
}
}
int sum = 0;
for (int i = 0; i <= G; ++i)
sum = (sum + dp[i][P]) % mod; return sum;
}
}

  

Analysis:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-879-profitable-schemes/

879. Profitable Schemes的更多相关文章

  1. [LeetCode] 879. Profitable Schemes 盈利方案

    There are G people in a gang, and a list of various crimes they could commit. The i-th crime generat ...

  2. [Swift]LeetCode879. 盈利计划 | Profitable Schemes

    There are G people in a gang, and a list of various crimes they could commit. The i-th crime generat ...

  3. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  4. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  7. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

  8. URL Schemes

    APP 被唤醒离不开对URL Schemes的认知. 苹果选择沙盒来保障用户的隐私和安全,但沙盒也阻碍了应用间合理的信息共享,于是有了 URL Schemes 这个解决办法. URL Schemes ...

  9. 你所知道好玩有趣的 iOS URL schemes 有哪些?

    QQ的url是 mqq:// 微信是weixin:// 淘宝taobao:// 点评dianping:// dianping://search 微博 sinaweibo:// 名片全能王camcard ...

随机推荐

  1. HDU_5688

    /* 度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字. ...

  2. 201621123008 《Java程序设计》第四周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:继承,多态. 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 2. 书面作业 1. ...

  3. 关于对象的 width offsetwidth availWidth scrollHeight

    别人总结的.自己记不住,所以留着 了 offsetWidth 包含了对象的边线的宽度width 若你不在html 代码里明确指定这个值,那它的返回值会不一样,如果设置了width 则一样. widht ...

  4. 关闭文件流--fclose,

    头文件:#include<stdio.h> 函数原型:int fclose(FILE *fp) 参数说明:fp将被关闭的文件指针 返回值:成功返回0,失败返回EOF宏.

  5. 复制文件描述符---dup

    函数功能:复制文件描述符 头文件:#include<unistd.h> 函数原型:int dup(int oldfd) 参数说明:oldfd:旧的文件描述符 返回值:成功返回-个新的文件描 ...

  6. 2018.10.19 NOIP模拟 硬币(矩阵快速幂优化dp)

    传送门 不得不说神仙出题人DZYODZYODZYO出的题是真的妙. f[i][j][k]f[i][j][k]f[i][j][k]表示选的硬币最大面值为iii最小面值不小于jjj,总面值为kkk时的选法 ...

  7. 2018.07.23[PA2015]Siano(线段树)

    [PA2015]Siano 描述 Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会长高a[i]厘 ...

  8. git将本地仓库强制替换掉远程仓库

    $ git remote add origin <url> $ git push --force --set-upstream origin master

  9. ViewFlipper实现自动播放的图片库

    作者实现的基础上,加上了文字的变换 public class MainActivity extends Activity { private ViewFlipper viewFlipper; priv ...

  10. Windows.Web.Http.HttpClient.GetStringAsync 总是返回相同的结果

    今天在测试博客园新闻WP8.1客户端的时候,发现电脑上浏览的新闻已经更新了.但手机上的还没更新,于是想到肯定是有bug了.于是建了一个Web测试项目,发现只有第一次发出了请求.一开始以为是MVVM的问 ...