Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms Tutorial 27th Sept, 2019.

Similar presentations


Presentation on theme: "Algorithms Tutorial 27th Sept, 2019."— Presentation transcript:

1 Algorithms Tutorial 27th Sept, 2019

2 Question 1 You are given a sequence of n songs where the ith song is Li minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD1, CD2, CD3,....,CDk) where each CD can hold m minutes. Furthermore The songs must be recorded in the given order, song 1, song 2, ..., song n. All songs must be included. No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to and an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity.

3 Question 1 You are given a sequence of n songs where the ith song is Li minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD1, CD2, CD3,....,CDk) where each CD can hold m minutes. Furthermore, the songs must be recorded in the given order, song 1, song 2, ..., song n. All songs must be included. No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to and an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity. Hint-1 Try to think greedily about putting songs on CDs.

4 Question 1 You are given a sequence of n songs where the ith song is Li minutes long. You want to place all of the songs on an ordered series of CDs (e.g. CD1, CD2, CD3,....,CDk) where each CD can hold m minutes. Furthermore, the songs must be recorded in the given order, song 1, song 2, ..., song n. All songs must be included. No song may be split across CDs. Your goal is to determine how to place them on the CDs as to minimize the number of CDs needed. Give the most efficient algorithm you can to and an optimal solution for this problem, prove the algorithm is correct and analyze the time complexity. Hint-2 Try to fit as many songs as possible on one CD.

5 Question 1 Solution: Put as many songs (from song 1 to song g) on the 1st CD as possible without exceeding the m minute limit. Then iteratively repeat this procedure for the remaining CDs. Since this just requires keeping a running sum in which each of the n song lengths are included once, clearly the above is an O(n) algorithm.

6 Question 1 Sketch Proof: First let’s prove that first CD is optimal.
Let S be an optimal solution in which the 1st CD holds songs 1 to f. The greedy algorithm puts songs 1 to g on the 1st CD. We now prove there is an optimal solution which puts the 1st g songs on the 1st CD. If f = g then S is such a solution. Now suppose that f ≠ g. Since the greedy algorithm puts as many songs on the 1st CD as possible, it follows that f < g. We construct a solution S’ by modifying S to move all songs from f + 1 to g onto the 1st CD. We now argue that S’ is a legal solution which is optimal. First note that the number of CDs used by S’ is at most the number of CDs used by S. All that remains is to argue that S’ is legal (i.e. no CD holds more than m minutes of songs). By definition of the greedy choice the 1st CD can hold songs 1 to g. Since S is a legal solution all of the CDs in it hold at most m minutes. For all but CD 1 the number of songs is only reduced and hence must still hold at most m minutes. Hence S’ is legal.

7 Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on.

8 Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Hint 1: You will never need to press the same switch twice. Why? Because it is equivalent to not pressing the switch and you will end up with the same state as before. So we can always solve the problem in at most n switch flips.

9 Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Hint 2: The order in which you press the switch does not affect the final state.

10 Question 2 N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Hint 2: The order in which you press the switch does not affect the final state.

11 Question 2 Solution Approach:
N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times. Note : 0 represents the bulb is off and 1 represents the bulb is on. Solution Approach: Therefore we can choose a particular order. To make things easier let's go from left to right. At the current position if the bulb is on after pressing the switches to its left, we move to the right, else we switch it on. This works because changing any switch to the right of it will not affect it anymore.

12 Question 3 You are manager of Grand Budapest Hotel. You are given the arrival and departure time of each guest. But your cleaning staff is lazy. So you have to instruct them to clean minimum number of rooms. If there are two guests and their stay in the hotel does not coincide then you need only one cleaned room because after 1st guest leaves you can give the same room to 2nd guest. So for given arrival and departure time of each guest design optimal algorithm to return minimum number of rooms to be cleaned by the cleaning staff. Analyze the time complexity of the algorithm. You may assume departure time is strictly greater than arrival time and all time instances provided are distinct.

13 Question 3 You are manager of Grand Budapest Hotel. You are given the arrival and departure time of each guest. But your cleaning staff is lazy. So you have to instruct them to clean minimum number of rooms. If there are two guests and their stay in the hotel does not coincide then you need only one cleaned room because after 1st guest leaves you can give the same room to 2nd guest. So for given arrival and departure time of each guest design optimal algorithm to return minimum number of rooms to be cleaned by the cleaning staff. Analyze the time complexity of the algorithm. You may assume departure time is strictly greater than arrival time and all time instances provided are distinct. Hint-1 Try Using Sorting.

14 Question 3 You are manager of Grand Budapest Hotel. You are given the arrival and departure time of each guest. But your cleaning staff is lazy. So you have to instruct them to clean minimum number of rooms. If there are two guests and their stay in the hotel does not coincide then you need only one cleaned room because after 1st guest leaves you can give the same room to 2nd guest. So for given arrival and departure time of each guest design optimal algorithm to return minimum number of rooms to be cleaned by the cleaning staff. Analyze the time complexity of the algorithm. You may assume departure time is strictly greater than arrival time and all time instances provided are distinct. Hint-2 Try to go chronologically/greedily along each event.

15 Question 3 Input: (a1,d1) , (a2,d2) , …..
Convert input in individual event pair <time,arrival/departure> . (ie. (a1,arrival) , (d1,departure) ….) Sort all events pair based on time(first element of pair). max = 0 current_guests = 0 For all events pairs If event second element == arrival current_guests++ else current_guests-- If current_guests > max max = current_guests Return max Time Complexity: O(nlogn)

16 Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

17 Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Approach: It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it better and possibly in a single pass? (PS: The solution shouldn’t depend on the size of integer ) Space complexity should be O(n).

18 Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Hint 1: Suppose you had the solution for all numbers smaller than n, then can you find it for n using them?

19 Question 4 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Solution Approach: Suppose the number is If you right shift it one place, you will get a smaller value whose number of bits have already been computed and you only need to check now for the units place in the original number.

20 Question 5 Given an array A, of N integers.
Return the highest product possible by multiplying 3 numbers from the array.

21 Question 5 Given an array A, of N integers.
Return the highest product possible by multiplying 3 numbers from the array. Hint 1: Do we need to consider all the elements from the array ? Is it enough to consider just the 3 maximum numbers from the array ?

22 Question 5 Given an array A, of N integers.
Return the highest product possible by multiplying 3 numbers from the array. Hint 1: Do we need to consider all the elements from the array ? Is it enough to consider just the 3 maximum numbers from the array ?

23 Question 5 Given an array A, of N integers.
Return the highest product possible by multiplying 3 numbers from the array. Hint 2: Obviously No. Product of 2 negative numbers is positive. So, Negative numbers with higher absolute value might also be of interest. How about maximum 3 elements, and 2 negative elements with the highest absolute value ?

24 Question 5 Choosing 3 maximum elements in the array and 2 negative elements with the highest absolute value should be enough. Once you have the 5 elements you desire, your answer would be one of the following : 1) Product of 3 maximum elements 2) Product of the 2 negative elements with max absolute value and maximum positive value.

25 Question 6 There are N Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x − 1. Any of these moves consumes 1 minute. There can be any number of moves. Assign mice to holes so that the time when the last mouse gets inside a hole is minimized.

26 Question 6 There are N Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x − 1. Any of these moves consumes 1 minute. There can be any number of moves. Assign mice to holes so that the time when the last mouse gets inside a hole is minimized. Hint: Sorting

27 Question 6 Solution: Sort mice positions
Sort hole positions (in the same order) Get maximum value of |mice[i]-hole[i]|


Download ppt "Algorithms Tutorial 27th Sept, 2019."

Similar presentations


Ads by Google