Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SE103 - Week 9, Session 1

Caren
August 08, 2020

SE103 - Week 9, Session 1

Caren

August 08, 2020
Tweet

More Decks by Caren

Other Decks in Education

Transcript

  1. 1. Break down into overlapping subproblems 2. Cache results to

    avoid solving same subproblems What is Dynamic Programming
  2. 1. Break down into overlapping subproblems 2. Cache results to

    avoid solving same subproblems What is Dynamic Programming “Dynamic Programming was basically a phrase made up to confuse the government about what RAND was doing.” - Richard Bellman, ‘father’ of dynamic programming
  3. Sounds easy in theory, but way harder in practice! Why

    is Dynamic Programming so difficult?
  4. Sounds easy in theory, but way harder in practice! Like

    all other problems, the key is to recognize common patterns Why is Dynamic Programming so difficult?
  5. Sounds easy in theory, but way harder in practice! Like

    all other problems, the key is to recognize common patterns Continuous practice is critical - you’re probably not going to master dynamic programming in 2 weeks. Why is Dynamic Programming so difficult?
  6. Fibonacci 0, 1, 1, 2, 3, 5, 8, 13, 21,

    34, 55… To get the nth fib number … add the previous two numbers
  7. Fibonacci Recursive: public int CalculateFibonacci(int n) { if(n < 2)

    return n; return CalculateFibonacci(n-1) + CalculateFibonacci(n-2); } Run time: ??
  8. Fibonacci Recursive: public int CalculateFibonacci(int n) { if(n < 2)

    return n; return CalculateFibonacci(n-1) + CalculateFibonacci(n-2); } Run time: 2^n
  9. Fibonacci 0 1 1 What if we built up the

    fibonacci sequence first?
  10. Fibonacci What if we built up the fibonacci sequence first?

    0 1 1 2 3 5 8 int dp[] = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2]; return dp[n];
  11. Fibonacci What if we built up the fibonacci sequence first?

    0 1 1 2 3 5 8 int dp[] = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2]; return dp[n]; Run time: ??
  12. Fibonacci What if we built up the fibonacci sequence first?

    0 1 1 2 3 5 8 int dp[] = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) dp[i] = dp[i - 1] + dp[i - 2]; return dp[n]; Run time: n
  13. Given an array representation of coin values and a value

    n, return the number of ways to make change for the target amount n Given: {1, 5}, n = 6 Return: ?? Number of Ways to Make Change
  14. Given an array representation of coin values and a value

    n, return the number of ways to make change for the target amount n Given: {1, 5}, n = 6 Return: 2 { {6 x 1}, {5 x 1, 1 x 1} } Number of Ways to Make Change
  15. Let’s start by building an array from 0 -> n

    
 Given: {1, 5}, n = 6 Number of Ways to Make Change 0 1 2 3 4 5 6
  16. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 In each box, we’ll see how many ways we can make ’n’ with our coins 1, 5 Number of Ways to Make Change 0 1 2 3 4 5 6
  17. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 In each box, we’ll see how many ways we can make ’n’ with our coins 1, 5 Number of Ways to Make Change 0 1 2 3 4 5 6 1
  18. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 In each box, we’ll see how many ways we can make ’n’ with our coins 1, 5 Number of Ways to Make Change 0 1 2 3 4 5 6 1 1
  19. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 In each box, we’ll see how many ways we can make ’n’ with our coins 1, 5 Number of Ways to Make Change 0 1 2 3 4 5 6 1 1 1 1 1
  20. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 In each box, we’ll see how many ways we can make ’n’ with our coins 1, 5 Number of Ways to Make Change 0 1 2 3 4 5 6 1 1 1 1 1 2 2
  21. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 Let’s build up our array one ‘coin’ at a time (one pass with 1, second pass with 5) Number of Ways to Make Change 0 1 2 3 4 5 6 1 1 1 1 1 2 2
  22. Let’s start by building an array from 0 -> n

    Given: {1, 5}, n = 6 if (coinValue <= amount) {
 ways[amount] =
 ways[amount] + ways[amount - coinValue];
 } Number of Ways to Make Change 0 1 2 3 4 5 6 1 1 1 1 1 2 2
  23. Number of Ways to Make Change General Pattern: - Create

    an array to store solutions to ‘subproblems’ - what would be the solution up to ‘this’ point?
  24. Number of Ways to Make Change General Pattern: - Create

    an array to store solutions to ‘subproblems’ - what would be the solution up to ‘this’ point? - Use stored solutions to populate future elements of the array
  25. Number of Ways to Make Change General Pattern: - Create

    an array to store solutions to ‘subproblems’ - what would be the solution up to ‘this’ point? - Use stored solutions to populate future elements of the array - writing out the arrray and populating it by hand often helps you come up with the algorithm later
  26. Breakout Rooms - Half of group does one problem, other

    half does the other problem - 30 minutes, everybody tries to do their own problem - 10 minutes - group 1 explains their problem - 10 minutes - group 2 explains their problem - Last 10 minutes - everybody talks about the last problem and brainstorm what the dp array would look like
  27. Questions for me? Work Life Balance - How does the

    team figure out what to work on in the next 3-6 months? - How does the team tackle tech debt? - What is a typical release process? - Weekly? Biweekly? - How does the team prepare for releases? - How is the release monitored afterwards? - Are there oncall schedules?
  28. Questions for me? Career Growth - What do you see

    me working on in my first 3-6 months on the job? - How do people on the team get feedback usually?
  29. Questions for me? General - What are some challenging projects

    you’ve worked on in the past 3-6 months? - What kind of meetings do you usually have in a week?