Algorithms (CSCI 3104, Fall 2016)In this page, we will post new challenge problems and their solutions. For each challenge problems, students are invited to send their solutions to the instructor. Note that
Fall 2016 ProblemsWeek 3: Find max occupancyWe are given the everyday airplane schedule at a busy international
airport serving
Suppose we are given a large list containing pairs of landing and takeoff times for
each plane, expressed in minutes after midnight. Write an efficient algorithm
that will present the airport manager information about the number of planes in the
airport during the various time intervals of time. In particular, the table must be
sorted by increasing times, should cover the entire
Week 1: Find smallest missing positive number in an array.You are given an array of Your algorithm should run in linear time, ie Successfully solved: Byron Becker and Chance Roberts SolutionFirst, we can find the minimum element of the array Suppose the new array has code
def findSmallestMissing(a): n = len(a) m = min(a) for i in range(0,n): a[i] = a[i] -m To do so, the algorithm maintains two counters Invariant
For the subarray code
i = 0 n = len(a) while (i < n): if (a[i] == i): # a[i] already equals i, nothing more to do i=i+1 else: if (a[i] < i): # a[i] is strictly less than i # This means a[i] is a repeated element and cannot be missing swap(a,i,n-1) # Place a[i] in the range of elements that cannot be the missing one n=n-1 # reduce n by 1 else: e=a[i] # Let us call e = a[i] if (e < n): if (a[e] == e): # if a[e] already has e in it, e cannot be missing swap(a,i,n-1) n = n -1 else: # otherwise, place a[e] = e and bring a[i] = a[e] e = a[i] swap(a,i,e) else: # if e >= n swap(a,i,n-1) # then a[i] cannot be missing n = n-1 Once this is done, we can search for the first element where Let us illustrate what happens when we call ’'findSmallestMissing([0,3,2,0,4,6,7,9,1,11,13,11])’’
Here the smallest missing element is Why does this work in linear time? Complexity Analysis
Each step of the loop, one of the following happens: (a) Using the three facts above, we can prove that the running time is |