Share
Embed
Transcript
46 slides
-
Projects Will be looking through submissions in the next few
days Late Submissions won’t be scored, but slack me your repo -
Projects Will be looking through submissions in the next few
days Late Submissions won’t be scored, but slack me your repo Incompletes / Strikes -
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 -
Greedy greedy approach : at every step we take the
best option for us with the current circumstances -
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 -
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? -
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? -
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? -
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) -
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. -
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 -
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? -
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) -
Greedy Common Greedy Problems: Jump Game Activity Selection Job Sequencing
/ Task Scheduler Huffman Coding (more advanced) -
Greedy Common approaches: Sort the input in a way that
would allow for a greedy approach Java’s Comparator comes in handy -
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 -
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. -
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. -
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 -
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 -
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 -
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? -
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 -
Task Scheduler Have a size 26 array coolDownTimeLeft, each index
represents the cool down time needed for a particular task. -
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. -
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 -
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 -
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 -
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. -
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 -
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 -
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? -
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 -
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 -
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. -
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? -
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 -
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. -
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 ??? -
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 -
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] -
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