1. Home
  2. Explore
  3. Computer Science
  4. Dynamic Programming Course Intro

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Dynamic Programming Course Intro

  • 37Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: DP Basics
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is dynamic programming?

Tap to flipClick to flipspace

A method for solving problems by breaking them into overlapping subproblems and reusing solutions to avoid redundant computation.

Or swipe the card

37 cards

  1. What is dynamic programming?

    A method for solving problems by breaking them into overlapping subproblems and reusing solutions to avoid redundant computation.

  2. What are the two main strategies for implementing dynamic programming?

    Memoization (top-down recursive with caching) and tabulation (bottom-up iterative table filling).

  3. What is the key characteristic of overlapping subproblems?

    The same subproblem appears multiple times when solving the larger problem.

  4. What is the general recipe for memoization?

    1. Make it work: implement a recursive brute-force solution. 2. Make it efficient: add a memo object (hash map) to store and reuse results of subproblems.
  5. What is the general recipe for tabulation?

    1. Visualize the problem as a table. 2. Size the table based on inputs. 3. Initialize table with appropriate default values. 4. Seed the trivial base case. 5. Iterate through the table, filling future positions based on current position.
  6. How do you determine the time complexity of a recursive function using a recursion tree?

    Time complexity = (branching factor)^(height) where branching factor is the number of recursive calls per node and height is the maximum depth of recursion.

  7. How do you determine the space complexity (stack) of a recursive function?

    The space used by the call stack is equal to the height of the recursion tree (maximum depth), because stack frames are added and removed along a single path.

  8. What is the time and space complexity of the brute-force recursive Fibonacci?

    Time: O(2n)O(2^n)O(2n); Space: O(n)O(n)O(n) from call stack.

  9. What is the time and space complexity of memoized Fibonacci?

    Time: O(n)O(n)O(n); Space: O(n)O(n)O(n) from memo and stack.

  10. What is the time and space complexity of tabulated Fibonacci?

    Time: O(n)O(n)O(n); Space: O(n)O(n)O(n) (table of length n+1).

  11. In Fibonacci tabulation, what is the seed value and how is the table filled?

    Seed: table[1] = 1 (first Fibonacci number). The table is filled by iterating from left to right, adding the current value to the next two positions.

  12. What is the brute-force time complexity of grid traveler (m by n)?

    Time: O(2m+n)O(2^{m+n})O(2m+n); Space: O(m+n)O(m+n)O(m+n) from stack.

  13. What is the memoized time complexity of grid traveler?

    Time: O(mcdotn)O(m cdot n)O(mcdotn); Space: O(m+n)O(m+n)O(m+n) from stack and memo.

  14. What is the tabulated time complexity of grid traveler?

    Time: O(mcdotn)O(m cdot n)O(mcdotn); Space: O(mcdotn)O(m cdot n)O(mcdotn) for the 2D table.

  15. In grid traveler tabulation, what is the seed and how is the table filled?

    Seed: table[1][1] = 1. Table is filled by iterating each cell and adding its value to the cell to the right (i, j+1) and below (i+1, j).

  16. What type of problem is canSum? (decision, combinatoric, optimization?)

    Decision problem: returns true/false if target sum can be generated.

  17. What is the brute-force time complexity of canSum (target m, array length n)?

    Time: O(nm)O(n^m)O(nm); Space: O(m)O(m)O(m) from stack.

  18. What is the memoized time complexity of canSum?

    Time: O(mcdotn)O(m cdot n)O(mcdotn); Space: O(m)O(m)O(m) from stack and memo.

  19. What is the tabulated time complexity of canSum?

    Time: O(mcdotn)O(m cdot n)O(mcdotn); Space: O(m)O(m)O(m) for the table.

  20. In canSum tabulation, what is the seed value and how is the table filled?

    Seed: table[0] = true. For each i from 0 to target, if table[i] is true, set table[i+num] = true for each num in numbers.

  21. What type of problem is howSum?

    Combinatoric problem: returns one combination (array) of numbers that sum to target, or null if impossible.

  22. What is the brute-force time complexity of howSum?

    Time: O(nmcdotm)O(n^m cdot m)O(nmcdotm); Space: O(m)O(m)O(m) from stack.

  23. What is the memoized time complexity of howSum?

    Time: O(mcdotncdotm)=O(m2n)O(m cdot n cdot m) = O(m^2 n)O(mcdotncdotm)=O(m2n); Space: O(m2)O(m^2)O(m2) from memo and stack.

  24. In howSum tabulation, what is the seed and how is the table filled?

    Seed: table[0] = []. For each i where table[i] is not null, for each num, copy table[i] and add num to form a new combination for table[i+num] (if not already set).

  25. What type of problem is bestSum?

    Optimization problem: returns the shortest combination that sums to target, or null if impossible.

  26. How does bestSum tabulation differ from howSum tabulation?

    When filling the table, if both current and new combination exist, keep the shorter one.

  27. What is the time complexity of bestSum tabulation?

    Time: O(m2n)O(m^2 n)O(m2n); Space: O(m2)O(m^2)O(m2).

  28. What is the key idea in canConstruct?

    Check if target string can be formed by concatenating words from a word bank (can reuse words). Returns boolean.

  29. In canConstruct tabulation, how is the table indexed?

    Table of length target.length + 1. Index i represents the prefix of target from 0 to i (exclusive). table[0] corresponds to empty string.

  30. In canConstruct tabulation, what is the seed and how is the table filled?

    Seed: table[0] = true. For each i where table[i] is true, for each word that matches the substring target.slice(i, i+word.length), set table[i+word.length] = true.

  31. What is the time complexity of canConstruct tabulation?

    Time: O(m2n)O(m^2 n)O(m2n) where m = target length, n = number of words; Space: O(m)O(m)O(m).

  32. What does countConstruct return?

    The number of distinct ways to construct the target string from the word bank.

  33. In countConstruct tabulation, what is the seed and how is the table filled?

    Seed: table[0] = 1. For each i, if table[i] > 0, for each matching word, add table[i] to table[i+word.length].

  34. In allConstruct tabulation, what is the seed and how is the table filled?

    Seed: table[0] = [[]] (2D array containing empty combination). For each i where table[i] is non-empty, for each matching word, for each combination in table[i], create a new combination with word appended and push to table[i+word.length].

  35. Why is allConstruct inherently exponential?

    The output itself may contain an exponential number of combinations; we must generate all of them.

  36. How do you encode multiple arguments in a memo object?

    Combine them into a string key, e.g., ${arg1},${arg2} to avoid collisions.

  37. What is a common mistake in tabulation regarding array initialization?

    Using .fill(new Array(...)) causing all elements to share the same array reference.

Card 1 of 37
Card 1 of 37

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent