dp
with dimensions [M+1][N+1][minProfit+1]
to 0.dp[M][j][0]
to 1 for all j
from 0 to N
, as there is always 1 way to achieve 0 profit regardless of the number of members.M-1
to 0), each member count (from 0 to N
), and each profit level (from 0 to minProfit
).(i, j, k)
, update dp[i][j][k]
by adding the number of ways to achieve k
profit without the current crime (dp[i+1][j][k]
) and, if possible, the number of ways to achieve k-profit[i]
profit with the current crime (dp[i+1][j-group[i]][max(0, k-profit[i])]
).N
when adding the current crime.dp[0][0][minProfit]
as the final answer.