Caren
August 08, 2020
490

SE103 - Week 9, Session 1

August 08, 2020

Transcript

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

avoid solving same subproblems What is Dynamic Programming
4. 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

7. Sounds easy in theory, but way harder in practice! Why

is Dynamic Programming so difﬁcult?
8. 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 difﬁcult?
9. 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 difﬁcult?
10. - Fibonacci - Knapsack - Unbounded knapsack - Palindromic Sequences

- Common Substrings Common Patterns in DP
11. - Fibonacci - Knapsack - Unbounded knapsack - Palindromic Sequences

- Common Substrings Common Patterns in DP
12. Fibonacci 0, 1, 1, 2, 3, 5, 8, 13, 21,

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

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

return n; return CalculateFibonacci(n-1) + CalculateFibonacci(n-2); } Run time: 2^n

16. Fibonacci 0 1 What if we built up the ﬁbonacci

sequence ﬁrst?
17. Fibonacci 0 1 1 What if we built up the

ﬁbonacci sequence ﬁrst?
18. Fibonacci What if we built up the ﬁbonacci sequence ﬁrst?

0 1 1 2 3 5 8
19. Fibonacci What if we built up the ﬁbonacci sequence ﬁrst?

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];
20. Fibonacci What if we built up the ﬁbonacci sequence ﬁrst?

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: ??
21. Fibonacci What if we built up the ﬁbonacci sequence ﬁrst?

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
22. 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
23. 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
24. 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
25. 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
26. 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
27. 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
28. 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
29. 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
30. 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
31. 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
32. 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?
33. 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
34. 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
35. 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

41. Questions for me? Work Life Balance - How does the

team ﬁgure 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?

43. Questions for me? Career Growth - What do you see

me working on in my ﬁrst 3-6 months on the job? - How do people on the team get feedback usually?
44. 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?