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 ...
随机推荐
- vim之quickfix
[vim之quickfix] quickfix功能将编译过程中产生的错误信息保存到文件中,然后vim利用这些信息跳转到源文件的对应位置,我们就可以进行错误的修正,之后跳到下一个错误重复上述操作,从而极 ...
- java Arrays.asList用法
java Arrays.asList用法 用途 Arrays是java容器相关操作的工具类,asList方法将Array转换为list,是Array和List之间的桥梁. 注意 Arrays.asLi ...
- 21个ui设计技巧,让你的设计不落伍
1.功能性极简主义 不少移动端APP和网站开始基于极简主义设计风来设计,而极简主义本身并非关注所有的信息,而是通过减少非关键信息来突出特定的内容,它是有着极强的功能性和偏向的.它有着如下的特征: ・简 ...
- Java数据结构和算法(二)顺序存储的树结构
Java数据结构和算法(二)顺序存储的树结构 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 二叉树也可以用数组存储,可以和完 ...
- Git使用1
1.先配置本地Git E:\personal>git config –-global user.name "lewy" E:\personal>git config – ...
- 前端之css语法3
一 float属性 1 基本的浮动规则: block元素和inline元素在文档流中的排列方式. block元素通常被现实独立的一块,独占一行.多个block元素会各自新起一行,默认block预算宽度 ...
- 使用Volley上传文件
使用浏览器上传文件,然后通过Wireshark抓包分析,发现发送的数据大概是这个样子. MIME Multipart Media Encapsulation, Type: multipart/form ...
- oracle创建新的用户 创建序列 并生成自动自增
1.用有dba权限的用户登录:sys用户 system 口令:manager 2.创建一个新用户:create user abc identified by 123456; 3.授予DBA权限: gr ...
- (线段树)Just a Hook -- hdu -- 1689
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 思路: 我的想法很简单,像上一题一样从后面向前面来算,前面已经覆盖的,后面自然不能再来计算了,具体 ...
- windows下用C++获取本机IP地址
BSTR CamUtilsCtrl::GET_TERM_IP(void){ AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strResult ...