其他分享
首页 > 其他分享> > 【879. 盈利计划】 动态规划

【879. 盈利计划】 动态规划

作者:互联网

//空间可以降到2维

class Solution { private int MOD= 1000000007; public int profitableSchemes(int n, int minProfit, int[] g, int[] profit) { int len = profit.length; long[][][] dp = new long[n+1][minProfit + 1][len]; for (int p = 0; p <= minProfit; p++) { for (int j = 0; j < len; j++) { dp[0][p][j] = 0; } } //初始化 for (int n1 = 0; n1 <= n; n1++) { dp[n1][0][0] = 1; if (n1 >= g[0]) { dp[n1][0][0] = 2; } for (int i = 1; i < len; i++) { if (g[i] > n1) { dp[n1][0][i] = dp[n1][0][i - 1]; } else { dp[n1][0][i] = dp[n1 - g[i]][0][i - 1] + dp[n1][0][i - 1]; } dp[n1][0][i] %= MOD; } } //递推 for (int n1 = 0; n1 <= n; n1++) { for (int p = 1; p <= minProfit; p++) { if( n1 >=g[0]){ if( p<=profit[0]){ dp[n1][p][0] = 1; }else{ dp[n1][p][0] = 0; } }else{ dp[n1][p][0] =0; } for (int i = 1; i < len; i++) { if (n1 >= g[i]) { if (p <= profit[i]) { dp[n1][p][i] = dp[n1 - g[i]][0][i - 1] + dp[n1][p][i - 1]; } else { dp[n1][p][i] = dp[n1 - g[i]][p - profit[i]][i - 1] + dp[n1][p][i - 1]; } } else { dp[n1][p][i] = dp[n1][p][i-1]; } dp[n1][p][i] %= MOD; } } } return (int)dp[n][minProfit][len-1]; } }

 

标签:879,int,len,profit,long,n1,动态,规划,dp
来源: https://www.cnblogs.com/fishcanfly/p/16340179.html