File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ MAX_LIMIT
2+ CHOICE[C, D]
3+
4+ dp[CHOICE, MAX_LIMIT] = 0
5+
6+ for (d: CHOICE[0]) {
7+ dp[0, MAX_LIMIT - CHOICE[0, d]] = 1
8+ }
9+
10+ for (i = 1; i < CHOICE; ++i) {
11+ for (j = 0; j < MAX_LIMIT; ++j) {
12+ if (dp[i-1, j] == 1) {
13+ for (k = 0; k < D; ++k) {
14+ if (j - CHOICE[i][k] >= 0) {
15+ dp[i, j - CHOICE[i][k]] = 1
16+ }
17+ }
18+ }
19+ }
20+ }
21+
22+ // print 1 closest to 0.
Original file line number Diff line number Diff line change 1+ MAX_LIMIT
2+ CHOICE
3+
4+ memo[MAX_LIMIT, choice]
5+
6+ dp(cost, choice) {
7+ if (cost < 0) {
8+ return -inf;
9+ }
10+
11+ if (choice >= CHOICE) {
12+ return MAX_LIMIT - cost;
13+ }
14+
15+ if (memo[cost, choice] != -1) { // -1 implies not found
16+ return memo[cost, choice];
17+ }
18+
19+ ans = -1
20+ for (c : CHOICE[choice]) {
21+ ans = max(ans, dp(cost - CHOICE[c], choice + 1));
22+ }
23+
24+ return memo[cost, choice] = ans;
25+ }
You can’t perform that action at this time.
0 commit comments