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
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 <= G <= 100
0 <= P <= 100
1 <= group[i] <= 100
0 <= profit[i] <= 100
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的更多相关文章
- [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 ...
随机推荐
- springmvc使用list集合实现商品列表的批量修改
1将表单的数据绑定到List 1.1 需求 实现商品数据的批量修改. 1.2 需求分析 要想实现商品数据的批量修改,需要在商品列表中可以对商品信息进行修改,饼干且可以批量提交修改后的商品数据. 1.3 ...
- qstring转string
Qt的QString功能丰富,对非英语语言的支持也不是问题,但支持得不够直接.例如,像 1 QString str("死亡使者赛维"); 这样直接用带中文的字符串进行构造,那么用Q ...
- process概念
multiprocess: multiprocess.cpu_count():统计cpu核数 multiprocess.active_chirdren():获取所有的子进程 multiprocess. ...
- 摹客项目在2018年工信部"创客中国"名列10强并荣获二等奖
2018“创客中国”互联网+大数据创新创业大赛(暨2018创客中国产业投资峰会)8月19日在厦门进行了总决赛.大赛由国家工业和信息化部.厦门市人民政府主办,厦门文广集团等承办.工信部信息中心领导.厦门 ...
- 关于某一discuz 6.0论坛故障的记录
某日,额突然发现公司的discuz 6.0论坛在IE6浏览器下面显示出现问题: 每个链接都出现了下划线,很难看,不过已访问的链接(a:visted)却没有下划线,于是怀疑css.htm有问题.于是对c ...
- 28. Bad Influence of Western Diet 西式饮食的消极影响
28. Bad Influence of Western Diet 西式饮食的消极影响 ① The spread of Western eating habits around the world i ...
- Docker 技巧:删除 Docker 容器和镜像
默认安装完 docker 后,每次执行 docker 都需要运行 sudo 命令,非常浪费时间影响效率.如果不跟 sudo,直接执行 docker images 命令会有如下问题: Get http: ...
- @media screen
参考地址: http://www.swordair.com/blog/2010/08/431/ http://ashaochangfu.blog.163.com/blog/static/1042517 ...
- hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- CodeForces - 589J —(DFS)
Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schema ...