Embercode
✕
Dynamic ProgrammingQuick quiz

Unbounded knapsack

6 questions. Covers Unbounded knapsack and rod cutting.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``cpp #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> w = {2, 5}, v = {3, 7}; int cap = 9; vector<int> dp(cap + 1, 0); for (int i = 0; i < 2; i++) for (int c = w[i]; c <= cap; c++) dp[c] = max(dp[c], dp[c - w[i]] + v[i]); cout << dp[cap] << endl; } ``
  2. 2. What does this program print? ``cpp #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { vector<int> price = {0, 1, 3, 4}; int n = 6; vector<int> best(n + 1, 0); for (int piece = 1; piece <= 3; piece++) for (int len = n; len >= piece; len--) best[len] = max(best[len], best[len - piece] + price[piece]); cout << best[n] << endl; } ``
  3. 3. In the rod-cutting program, what does first[len] store?
  4. 4. Which statement about the 0/1 knapsack loop and the unbounded knapsack loop is true?
  5. 5. For an unbounded knapsack, which way does the capacity loop run? Answer with one word.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.