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?
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?
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)
best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difﬁculty lies in recognizing whether a problem can be correctly solved greedily.
best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difﬁculty 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
best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difﬁculty 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?
best option for us with the current circumstances When appropriate, the greedy approach is a great way to solve a problem. However, the difﬁculty 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)
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 ﬁnish 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 ﬁnish 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.
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 ﬁnish 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 ﬁnish 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.
= 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
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
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.
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
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
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 inefﬁcient. We have to remove / mark tasks from the array when it’s scheduled. We have to repeatedly check if we can run a task
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 inefﬁcient. 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 inefﬁcient because we might end up scheduling frequent tasks in the end, which would cause us to use up unnecessary idle time.
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
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
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?
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
· · , An} of n activities with start and ﬁnish 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 Netﬂix -> (5pm - 10pm) Walk dog -> (5:30pm - 6pm) What’s the most activities we can schedule?
· · , An} of n activities with start and ﬁnish 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 Netﬂix -> (5pm - 10pm) Walk dog -> (5:30pm - 6pm) 4 -> Biking, Bookstore, Walk Dog, Dinner
· · , An} of n activities with start and ﬁnish times select max set S of “non-overlapping” activities. Greedy Sort activities by start time Pick ﬁrst activity after sorting Remove all activities with start time before ﬁnish time of chosen activity Recursively run on remaining activities.
· · , An} of n activities with start and ﬁnish times select max set S of “non-overlapping” activities. Sort activity by ﬁnish time [8am - 11pm] [9am-9:30am] [10am-11am] [11am-12pm]
· · , An} of n activities with start and ﬁnish times select max set S of “non-overlapping” activities. Similar problems: Scheduling Meeting Rooms Merging Intervals Course Scheduling