LeetCode879.盈利计划

帮派里有 G 名成员,他们可能犯下各种各样的罪行。

第 i 种犯罪会产生 profit[i] 的利润,它要求 group[i] 名成员共同参与。

让我们把这些犯罪的任何子集称为盈利计划,该计划至少产生 P 的利润。

有多少种方案可以选择?因为答案很大,所以返回它模 10^9 + 7 的值。

示例 1:

输入:G = 5, P = 3, group = [2,2], profit = [2,3]
输出:2
解释:
至少产生 3 的利润,该帮派可以犯下罪 0 和罪 1 ,或仅犯下罪 1 。
总的来说,有两种方案。
示例 2:

输入:G = 10, P = 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 = 1,方案数total= 0;如果profit[n] >= P,则total = total+1;
2、计算从n + 1、n+2、。。。profit.size()这个计划列表中,找出在总人数不超过G-group[n]的情况下,盈利超过P - profit[n]的情况数count
3、total += count,如果profit[n] >= P,则total++
4、n= n+1,如果n> profit.size()则退出,否则执行2
5、结束,返回total

代码如下:

	//盈利计划
	int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
		int result = 0;
		for (int i = 0; i < profit.size(); i++)
		{
			int number = group[i];
			int pro = profit[i];
			result += caculateSituationNumber(i + 1, group, profit, G - number, P - pro);
			if (pro >= P && number <= G)
				result++;
		}
		return result;
	}

	int caculateSituationNumber(int startIndex, vector<int>&group, vector<int>&profit, int G, int P) // startIndex表示第一个计划的索引位置,G是可以动用的总人数,P是总的盈利
	{
		if (startIndex >= profit.size())
			return 0;
		if (G <= 0)
			return 0;
		int result = 0;
		int pro = profit[startIndex];
		int gro = group[startIndex];
		if (pro >= P && gro <= G)
			result++;

		for (int i = startIndex + 1; i < profit.size(); i++)
		{
			int number = group[i - 1];
			int pro = profit[i - 1];
			result += caculateSituationNumber(i, group, profit, G - number, P - pro);
			if (profit[i] >= P && group[i] <= G)
				result++;
		}
		return result;
	}

运行之后,发现超时了,后改为用动态规划,
最早我所采用的动态规划的方法是在上面的思路之上修改的,改成了添加中间过程的计算,并且改掉了循环,代码如下:

	//盈利计划
	int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
		vector<vector<vector<int>>> res(101, vector<vector<int>>(101, vector<int>(101,0)));
		return caculateSituationNumber(0, group, profit, G, P, res);
	}

	int caculateSituationNumber(int startIndex, vector<int>&group, vector<int>&profit, int G, int P, vector<vector<vector<int>>>& res) // startIndex表示第一个计划的索引位置,G是可以动用的总人数,P是总的盈利
	{
		// 退出条件,类似斐波那契数列里面的Fib(0)和Fib(1)
		if (startIndex >= profit.size() && P <= 0 && G >= 0)
			return 1;
		if (startIndex >= profit.size())
			return 0;
		if (G < 0)
			return 0;
		if (res[G][P][startIndex] != 0)
			return res[G][P][startIndex];
		int result = 0;
		int pro = profit[startIndex];
		int gro = group[startIndex];
			
		// 先考虑做该任务的情况
		if (G >= gro)
		{
			int value = P - pro >= 0 ? P - pro : 0;
			result += caculateSituationNumber(startIndex + 1, group, profit, G - gro, value, res);
		}
		// 再考虑不做该任务的情况
		result = (result + caculateSituationNumber(startIndex + 1, group, profit, G, P, res)) % 1000000007;
		res[G][P][startIndex] = result;
		return result;
	}

可惜依旧超时。。。

后来参考了如下的视频:
LeetCode.879盈利计划
这里把它看成是一个二维的背包问题
这里的状态是当前的利润和当前使用的人数
代码如下:

	//盈利计划
	int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
		vector<vector<int>> dp(P + 1, vector<int>(G + 1, 0));
		dp[0][0] = 1;
		int K = group.size();
		for (int k = 0; k < K; k++)
		{
			int p = profit[k];
			int g = group[k];
			for (int i = P; i >= 0; --i)
			{
				int ip = min(P, i + p);
				for (int j = G - g; j >= 0; --j)
					dp[ip][j + g] = (dp[ip][j + g] + dp[i][j]) % 1000000007;
			}
		}
		return accumulate(begin(dp[P]), end(dp[P]), 0LL) % 1000000007;
	}

难了我好几天!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值