Untitled
unknown
plain_text
9 days ago
1.6 kB
2
Indexable
public class Solution { private static final int MOD = 1_000_000_007; public int profitableSchemes(int n, int minProfit, int[] group, int[] profit) { int[][][] memo = new int[group.length][n + 1][minProfit + 1]; for (int[][] layer : memo) { for (int[] row : layer) { Arrays.fill(row, -1); } } return dfs(0, n, 0, minProfit, group, profit, memo); } private int dfs(int index, int membersLeft, int currentProfit, int minProfit, int[] group, int[] profit, int[][][] memo) { // Base case: if we've processed all crimes if (index == group.length) { return currentProfit >= minProfit ? 1 : 0; } // If the result is already computed if (memo[index][membersLeft][currentProfit] != -1) { return memo[index][membersLeft][currentProfit]; } // Skip the current crime int skip = dfs(index + 1, membersLeft, currentProfit, minProfit, group, profit, memo); // Include the current crime, if possible int include = 0; if (membersLeft >= group[index]) { include = dfs(index + 1, membersLeft - group[index], Math.min(minProfit, currentProfit + profit[index]), minProfit, group, profit, memo); } // Store the result in the memo table memo[index][membersLeft][currentProfit] = (skip + include) % MOD; return memo[index][membersLeft][currentProfit]; } }
Editor is loading...
Leave a Comment