1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { static final int MOD = (int) 1e9 + 7; int n ; int[][] types, dp; Map<String, Integer> map = new HashMap<>();
public int waysToReachTarget(int target, int[][] types) { this.types = types; n = types.length; dp = new int[n][1002]; for (int[] arr : dp) Arrays.fill(arr, -1); return dfs(0, target); }
private int dfs(int index, int target) { if (index == n) return target == 0 ? 1 : 0; if (dp[index][target] != -1) return dp[index][target]; int res = 0, count = types[index][0], marks = types[index][1]; for (int i = 0; i <= Math.min(target / marks, count); ++i) res = (res + (dfs(index + 1, target - i * marks) % MOD)) % MOD; return dp[index][target] = res; } }
|