forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
58 lines (45 loc) · 1.44 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// Source : https://leetcode.com/problems/profitable-schemes/description/
/// Author : liuyubobobo
/// Time : 2018-08-04
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(G * P * n)
/// Space Complexity: O(G * P)
class Solution {
private:
int MOD = 1e9 + 7;
public:
int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
//vector<vector<vector<int>>> dp(2, vector<vector<int>>(G + 1, vector<int>(P + 1, -1)));
vector<vector<int>> dp(P + 1, vector<int>(G + 1, 0));
dp[0][0] = 1;
for(int i = 0; i < group.size() ; i ++){
for(int j = P; j >= 0; j --)
for(int k = G - group[i]; k >= 0; k --){
int p = min(P, j + profit[i]);
int g = k + group[i];
dp[p][g] += dp[j][k];
dp[p][g] %= MOD;
}
}
long long res = 0ll;
for(int x: dp[P]){
res += x;
res %= MOD;
}
return res;
}
};
int main() {
int G1 = 5, P1 = 3;
vector<int> group1 = {2, 2};
vector<int> profit1 = {2, 3};
cout << Solution().profitableSchemes(G1, P1, group1, profit1) << endl; // 2
int G2 = 10, P2 = 5;
vector<int> group2 = {2, 3, 5};
vector<int> profit2 = {6, 7, 8};
cout << Solution().profitableSchemes(G2, P2, group2, profit2) << endl; // 7
return 0;
}