
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Profitable Schemes in C++
Suppose there is a gang with G people and a list of various crimes they could commit. The i-th crime generates a profit value profit[i] and requires group[i] gang members to participate.
If a gang member is participating in one crime, that he can't participate in another crime. Now let us define profitable scheme any subset of these crimes that generates at least P profit, and total number of members participating in that subset of crimes is at most G.
We have to find how many schemes can be chosen? The answer may be very large, So return it modulo 10^9 + 7.
So, if the input is like G = 5, P = 3 and group = [2,2], profit = [2,3], then the output will be 2
To solve this, we will follow these steps −
ret := 0
Define one 2D array dp of size (G + 1) x (P + 1)
dp[0, 0] := 1
for initialize k := 0, when k < size of group, update (increase k by 1), do −
p := profit[k], g := group[k]
for initialize i := G - g, when i >= 0, update (decrease i by 1), do −
for initialize j := P, when j >= 0, update (decrease j by 1), do−
dp[i + g, minimum of P and j + p]
dp[i + g, minimum of P and j + p]
for initialize i := 0, when i <= G, update (increase i by 1), do −
ret := ret + dp[i, P]
ret := ret mod m
return ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; const int m = 1e9 + 7; class Solution { public: int profitableSchemes(int G, int P, vector<int> &group, vector<int> &lprofit) { int ret = 0; vector<vector<int>> dp(G + 1, vector<int>(P + 1)); dp[0][0] = 1; for (int k = 0; k < group.size(); k++) { int p = profit[k]; int g = group[k]; for (int i = G - g; i >= 0; i--) { for (int j = P; j >= 0; j--) { dp[i + g][min(P, j + p)] += dp[i][j]; dp[i + g][min(P, j + p)] %= m; } } } for (int i = 0; i <= G; i++) { ret += dp[i][P]; ret %= m; } return ret; } }; main(){ Solution ob; vector<int> v = {2,2}, v1 = {2,3}; cout << (ob.profitableSchemes(5,3,v, v1)); }
Input
5, 3, [2,2], [2,3]
Output
2
- Related Articles
- Data transfer schemes in 8085
- The URL Schemes
- What are Directory Schemes?
- Discuss the Data Transfer Schemes in Computer Architecture?
- The Importance of Color Schemes in Fashion Design
- How a holistic strategy drives profitable business growth?
- What are Vector-Access Memory Schemes in Computer Architecture?
- What is syntax-directed translation schemes in compiler design?
- Key Strategies to Maintain a Profitable Position in the Fashion Market
- What are the schemes of Branch Detection?
- What are the schemes of Branch prediction?
- What is Types of Syntax Directed Translation Schemes?
- What Digital Marketing Agency Business Model is the Most Profitable?
- Is it true that farming is no more a profitable business in India?
- How to apply pseudo color schemes to an image plot in Matplotlib?
