879:盈利计划
This commit is contained in:
parent
89f31d0bf7
commit
773929955e
81
src/main/java/leetcode/editor/cn/ProfitableSchemes.java
Normal file
81
src/main/java/leetcode/editor/cn/ProfitableSchemes.java
Normal file
@ -0,0 +1,81 @@
|
||||
//集团里有 n 名员工,他们可以完成各种各样的工作创造利润。
|
||||
//
|
||||
// 第 i 种工作会产生 profit[i] 的利润,它要求 group[i] 名成员共同参与。如果成员参与了其中一项工作,就不能参与另一项工作。
|
||||
//
|
||||
// 工作的任何至少产生 minProfit 利润的子集称为 盈利计划 。并且工作的成员总数最多为 n 。
|
||||
//
|
||||
// 有多少种计划可以选择?因为答案很大,所以 返回结果模 10^9 + 7 的值。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:n = 5, minProfit = 3, group = [2,2], profit = [2,3]
|
||||
//输出:2
|
||||
//解释:至少产生 3 的利润,该集团可以完成工作 0 和工作 1 ,或仅完成工作 1 。
|
||||
//总的来说,有两种计划。
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
|
||||
//输出:7
|
||||
//解释:至少产生 5 的利润,只要完成其中一种工作就行,所以该集团可以完成任何工作。
|
||||
//有 7 种可能的计划:(0),(1),(2),(0,1),(0,2),(1,2),以及 (0,1,2) 。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= n <= 100
|
||||
// 0 <= minProfit <= 100
|
||||
// 1 <= group.length <= 100
|
||||
// 1 <= group[i] <= 100
|
||||
// profit.length == group.length
|
||||
// 0 <= profit[i] <= 100
|
||||
//
|
||||
// Related Topics 动态规划
|
||||
// 👍 138 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//879:盈利计划
|
||||
public class ProfitableSchemes{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ProfitableSchemes().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int profitableSchemes(int n, int minProfit, int[] group, int[] profit) {
|
||||
int len = group.length, MOD = (int)1e9 + 7;
|
||||
int[][][] dp = new int[len + 1][n + 1][minProfit + 1];
|
||||
dp[0][0][0] = 1;
|
||||
for (int i = 1; i <= len; i++) {
|
||||
int members = group[i - 1], earn = profit[i - 1];
|
||||
for (int j = 0; j <= n; j++) {
|
||||
for (int k = 0; k <= minProfit; k++) {
|
||||
if (j < members) {
|
||||
dp[i][j][k] = dp[i - 1][j][k];
|
||||
} else {
|
||||
dp[i][j][k] = (dp[i - 1][j][k] + dp[i - 1][j - members][Math.max(0, k - earn)]) % MOD;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int sum = 0;
|
||||
for (int j = 0; j <= n; j++) {
|
||||
sum = (sum + dp[len][j][minProfit]) % MOD;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
43
src/main/java/leetcode/editor/cn/ProfitableSchemes.md
Normal file
43
src/main/java/leetcode/editor/cn/ProfitableSchemes.md
Normal file
@ -0,0 +1,43 @@
|
||||
<p>集团里有 <code>n</code> 名员工,他们可以完成各种各样的工作创造利润。</p>
|
||||
|
||||
<p>第 <code>i</code> 种工作会产生 <code>profit[i]</code> 的利润,它要求 <code>group[i]</code> 名成员共同参与。如果成员参与了其中一项工作,就不能参与另一项工作。</p>
|
||||
|
||||
<p>工作的任何至少产生 <code>minProfit</code> 利润的子集称为 <strong>盈利计划</strong> 。并且工作的成员总数最多为 <code>n</code> 。</p>
|
||||
|
||||
<p>有多少种计划可以选择?因为答案很大,所以<strong> 返回结果模 </strong><code>10^9 + 7</code><strong> 的值</strong>。</p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5, minProfit = 3, group = [2,2], profit = [2,3]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>至少产生 3 的利润,该集团可以完成工作 0 和工作 1 ,或仅完成工作 1 。
|
||||
总的来说,有两种计划。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 10, minProfit = 5, group = [2,3,5], profit = [6,7,8]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>至少产生 5 的利润,只要完成其中一种工作就行,所以该集团可以完成任何工作。
|
||||
有 7 种可能的计划:(0),(1),(2),(0,1),(0,2),(1,2),以及 (0,1,2) 。</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>0 <= minProfit <= 100</code></li>
|
||||
<li><code>1 <= group.length <= 100</code></li>
|
||||
<li><code>1 <= group[i] <= 100</code></li>
|
||||
<li><code>profit.length == group.length</code></li>
|
||||
<li><code>0 <= profit[i] <= 100</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>动态规划</li></div></div>\n<div><li>👍 180</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user