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

Greedy - Session 1

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=47 Caren
July 25, 2018
110

Greedy - Session 1

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=128

Caren

July 25, 2018
Tweet

Transcript

  1. Greedy

  2. Projects Will be looking through submissions in the next few

    days
  3. Projects Will be looking through submissions in the next few

    days Late Submissions
 won’t be scored, but slack me your repo
  4. Projects Will be looking through submissions in the next few

    days Late Submissions
 won’t be scored, but slack me your repo Incompletes / Strikes
  5. Projects Will be looking through submissions in the next few

    days Late Submissions
 won’t be scored, but slack me your repo Incompletes / Strikes Solutions guide on Course Portal
  6. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances
  7. Greedy Advantages: Simplicity: easier to visualize and code Efficiency: usually

    don't involve having to store a lot of data or going through multiple passes of the input
  8. Greedy Advantages: Simplicity: easier to visualize and code Efficiency: usually

    don't involve having to store a lot of data or going through multiple passes of the input Disadvantages: Hard to design: How can we manipulate the data so we can solve it with a greedy approach? Proving correctness: Will the greedy approach always work?
  9. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances Coin change problem:
 What is the least amount of coins we can use to add up to x cents?
  10. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances Coin change problem:
 What is the least amount of coins we can use to add up to x cents? For example: what’s the least amount of coins to make 53 cents?
  11. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances Coin change problem:
 What is the least amount of coins we can use to add up to x cents? For example: what’s the least amount of coins to make 53 cents? -> 4 coins (2 quarters, 3 pennies)
  12. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difficulty lies in recognizing whether a problem can be correctly solved greedily.
  13. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difficulty lies in recognizing whether a problem can be correctly solved greedily. What is the least amount of coins we can use to add up to x cents GIVEN the coins we have are:
 25cents, 20 cents, 1 cent
  14. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difficulty lies in recognizing whether a problem can be correctly solved greedily. What is the least amount of coins we can use to add up to x cents GIVEN the coins we have are:
 25cents, 20 cents, 1 cent For example: What’s the least amount of coins we can use to make 40 cents?
  15. Greedy greedy approach : at every step we take the

    best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difficulty lies in recognizing whether a problem can be correctly solved greedily. What is the least amount of coins we can use to add up to x cents GIVEN the coins we have are:
 25cents, 20 cents, 1 cent For example: What’s the least amount of coins we can use to make 40 cents? -> 2 (two 20 cents) , but if we were greedy, it would have been 1 (quarter) + 15 (pennies)
  16. Greedy Common Greedy Problems: Jump Game
 Activity Selection
 Job Sequencing

    / Task Scheduler
 Huffman Coding (more advanced)
  17. Greedy Common approaches: Sort the input in a way that

    would allow for a greedy approach
 Java’s Comparator comes in handy
  18. Greedy Common approaches: Sort the input in a way that

    would allow for a greedy approach
 Java’s Comparator comes in handy Keeping track of a ‘maxSoFar’ while iterating through the date set
  19. Task Scheduler Given a char array representing tasks CPU need

    to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. You need to return the least number of intervals the CPU will take to finish all the given tasks. Example 1:
 Input: tasks = ["A","A","A","B","B","B"], n = 2
 Output: 8
 Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
  20. Task Scheduler Given a char array representing tasks CPU need

    to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. You need to return the least number of intervals the CPU will take to finish all the given tasks. Example 1:
 Input: tasks = ["A","A","A","B","B","B"], n = 2
 Output: 8
 Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
  21. Task Scheduler Our own examples: Input: tasks = ["A","A"], n

    = 2

  22. Task Scheduler Our own examples: Input: tasks = ["A","A"], n

    = 2
 Output: 4
 Explanation: A -> idle -> idle -> A Input: tasks = [“A”,"A", “B”], n = 2

  23. Task Scheduler Our own examples: Input: tasks = ["A","A"], n

    = 2
 Output: 4
 Explanation: A -> idle -> idle -> A Input: tasks = [“A”,"A", “B”], n = 2
 Output: 4
 Explanation: A -> B -> idle -> A Input: tasks = [“A”,"A", “B”], n = 3
  24. Task Scheduler Our own examples: Input: tasks = ["A","A"], n

    = 2
 Output: 4
 Explanation: A -> idle -> idle -> A Input: tasks = [“A”,"A", “B”], n = 2
 Output: 4
 Explanation: A -> B -> idle -> A Input: tasks = [“A”,"A", “B”], n = 3
 Output: 5
 Explanation: A -> B -> idle -> idle -> A
  25. Task Scheduler We have to keep track of when a

    certain task (ie task A, task B) can be executed
 Keep track of how much cool down period is left until a task 
 can be executed Ideas?
  26. Task Scheduler We have to keep track of when a

    certain task (ie task A, task B) can be executed
 Keep track of how much cool down period is left until a task 
 can be executed Have a size 26 array, each index represents the cool down time needed for a particular task. For example, if our input was [“A”, “A”] , cool down time = 2
 Our array would start like [0, 0, … 0] (length is 26)
 When we schedule task A to run, our array would then look like
 [2, 0, … 0] since after we run task A we have to wait for 2 more iterations of cool down
  27. Task Scheduler Have a size 26 array coolDownTimeLeft, each index

    represents the cool down time needed for a particular task.
  28. Task Scheduler Have a size 26 array coolDownTimeLeft, each index

    represents the cool down time needed for a particular task. Iterate through each task. When we want to try and schedule a task to run, check the array to see if that’s allowed.
  29. Task Scheduler Have a size 26 array coolDownTimeLeft, each index

    represents the cool down time needed for a particular task. Iterate through each task. When we want to try and schedule a task to run, check the array to see if that’s allowed. Update coolDownTimeLeft after every iteration
  30. Task Scheduler Have a size 26 array coolDownTimeLeft, each index

    represents the cool down time needed for a particular task. Iterate through each task. When we want to try and schedule a task to run, check the array to see if that’s allowed. Update coolDownTimeLeft after every iteration [A, A, B, C] 
 schedule A - > [2, 0, 0 .. 0]
 try to schedule A, can’t. schedule B instead -> [1, 2, 0 .. 0]
 try to schedule A, can’t. schedule C instead -> [0, 1, 2 .. 0]
 try to schedule A again -> [2, 0, 1, 0]
 A -> B -> C -> A
  31. Task Scheduler Have a size 26 array coolDownTimeLeft, each index

    represents the cool down time needed for a particular task. Iterate through each task. When we want to try and schedule a task to run, check the array to see if that’s allowed. Update coolDownTimeLeft after every iteration This seems to work, but it also seems inefficient.
 We have to remove / mark tasks from the array when it’s scheduled.
 We have to repeatedly check if we can run a task
  32. Task Scheduler Have a size 26 array coolDownTimeLeft, each index

    represents the cool down time needed for a particular task. Iterate through each task. When we want to try and schedule a task to run, check the array to see if that’s allowed. Update coolDownTimeLeft after every iteration This seems to work, but it also seems inefficient.
 We have to remove / mark tasks from the array when it’s scheduled.
 We have to repeatedly check if we can run a task This may also be inefficient because we might end up scheduling frequent tasks in the end, which would cause us to use up unnecessary idle time.
  33. Collaborative Studying Spend 30 minutes trying to solve the problem

    on your own
 It doesn’t have to be the most optimal solution to start (ie : the n^2 solution)
 From there, iterate on your solution to make it more optimal
  34. Collaborative Studying Spend 30 minutes trying to solve the problem

    on your own
 It doesn’t have to be the most optimal solution to start (ie : the n^2 solution)
 From there, iterate on your solution to make it more optimal If you’re still stuck after 30 minutes, read through some of the solutions to try and understand the approach
  35. Collaborative Studying Spend 30 minutes trying to solve the problem

    on your own
 It doesn’t have to be the most optimal solution to start (ie : the n^2 solution)
 From there, iterate on your solution to make it more optimal If you’re still stuck after 30 minutes, read through some of the solutions to try and understand the approach Spend 10 minutes to prepare how you would explain this problem to your partner. 
 How can you explain the problem with example inputs so your partner
 understands what we’re trying to solve for?
 What were the different approaches you took?
 What was the most optimal solution in the end?
 What are some tricky edge cases to consider?
  36. Collaborative Studying Spend 30 minutes trying to solve the problem

    on your own
 It doesn’t have to be the most optimal solution to start (ie : the n^2 solution)
 From there, iterate on your solution to make it more optimal If you’re still stuck after 30 minutes, read through some of the solutions to try and understand the approach Spend 10 minutes to prepare how you would explain this problem to your partner. 
 How can you explain the problem with example inputs so your partner
 understands what we’re trying to solve for?
 What were the different approaches you took?
 What was the most optimal solution in the end?
 What are some tricky edge cases to consider? Present to each other about the problem you worked on
  37. Collaborative Studying Goals : 1) Get running solution in Leetcode

    2) Be able to explain your approach and solution 3) Help your partner understand the solution as well
  38. Collaborative Studying

  39. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities.
  40. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Go Biking - > (9am - 12pm)
 Get Lunch -> (11am - 2pm)
 Visit Bookstore -> (12:30pm - 1pm)
 Grab Dinner -> (6-8pm)
 Watch Netflix -> (5pm - 10pm)
 Walk dog -> (5:30pm - 6pm) What’s the most activities we can schedule?
  41. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Go Biking - > (9am - 12pm)
 Get Lunch -> (11am - 2pm)
 Visit Bookstore -> (12:30pm - 1pm)
 Grab Dinner -> (6-8pm)
 Watch Netflix -> (5pm - 10pm)
 Walk dog -> (5:30pm - 6pm) 4 -> Biking, Bookstore, Walk Dog, Dinner
  42. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Greedy
 Sort activities by start time
 Pick first activity after sorting
 Remove all activities with start time before finish time of chosen activity
 Recursively run on remaining activities.
  43. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Sort activity by ???
  44. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Sort activity by finish time

  45. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Sort activity by finish time
 [8am - 11pm] [9am-9:30am] [10am-11am] [11am-12pm]
  46. Activity Selection Given a set A = {A1, A2, ·

    · · , An} of n activities with start and finish times select max set S of “non-overlapping” activities. Similar problems:
 Scheduling Meeting Rooms
 Merging Intervals
 Course Scheduling