879. Profitable Schemes
There are G people in a gang, and a list of various crimes they could commit.
The
i-th crime generates aprofit[i]and requiresgroup[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
Pprofit, 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 <= G <= 1000 <= P <= 1001 <= group[i] <= 1000 <= profit[i] <= 1001 <= 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的更多相关文章
- [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 ...
- [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 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
- URL Schemes
APP 被唤醒离不开对URL Schemes的认知. 苹果选择沙盒来保障用户的隐私和安全,但沙盒也阻碍了应用间合理的信息共享,于是有了 URL Schemes 这个解决办法. URL Schemes ...
- 你所知道好玩有趣的 iOS URL schemes 有哪些?
QQ的url是 mqq:// 微信是weixin:// 淘宝taobao:// 点评dianping:// dianping://search 微博 sinaweibo:// 名片全能王camcard ...
随机推荐
- 详解python2 和 python3的区别-乾颐堂
看到这个题目大家可能猜到了我接下来要讲些什么,呵呵,对了,那就是列出这两个不同版本间的却别!搜索一下大家就会知道,python有两个主要的版本,python2 和 python3 ,但是python又 ...
- linux bluez
Linux下开放的蓝牙协议栈主要包括IBM公司的BlueDrekar,Nokia公司的Affix, Axis公司的OpenBT和官方协议栈BlueZ.我们主要对Bluez进行探讨. BlueZ基础代码 ...
- 调用数据库-corina
pym = mysql(host='#####', port=3306, user='######', password='########', database='algorithm') #查询数据 ...
- windown 安装配置 mvn不是内部或外部命令
path 检查没有任何问题,就直接把maven路径直接放到path前面 如果提示JAVA_HOME not found 之类错误,环境变量中需要设置JAVA_HOME,而且需要在path中添加%JAV ...
- seo工具
http://tool.seowhy.com/ 一.关键词查词类工具:可以查询出更多目标客户可能搜索的词语 1.百度指数:http://index.baidu.com/ 这个工具是使用人数最多的 2. ...
- part1:2-嵌入式系统简单概念
1.3个特点+1个性质:以应用为中心.软硬件可裁剪.对功能-体积-功耗等有严格要求:专用的计算机系统. 应用领域: 软硬件可裁剪,是什么结构让嵌入式系统具备了这样的特点? 嵌入式系统的体系结构:硬件: ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- head first 设计模式文摘
1 欢迎来到设计模式世界:设计模式入门 2 让你的对象知悉现况:观察者模式 3 装饰对象:装饰者模式 4 工厂模式:烘烤OO的精华 5 单件模式:独一无二的对象 6 命令模式:封装调用 7 适配器模式 ...
- trsd_extract_EDSD_new
# -*- coding:utf-8 -*- import re ''' 适应新版本 ''' year='17A'#用户自定义 ss='./data/'#根目录 filename = ss+'EDSD ...
- (KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746
http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others ...