Advanced Algorithm Diary 1: The algorithm of the course __

Source: Internet
Author: User

Professor Shen left a mapping exercise before the first class. Here is a summary of this assignment.
The answers to the exercises are integrated: my own homework, the other students ' better homework and Professor Shen's explanation.

Dear All, our lecturer has assigned the I homework for background knowledge assessment, all of your are asked to Downlo Ad the homework (from Group sharing files) and send it to sunxiangguodut@qq.com after finishing. The due is May 26.
As long as you submit this homwork before due, you'll get the points. Problems with * Mark are not required. You can get extra points if you solve them. Homework 1

sorting, Divide & Conquer, greedy

Sun Xiang
161547
Sunxianguodut@qq.com

1 The number of cars produced by Toyota company in the past n years are stored to an array a[1], a[2], ..., a[n. We wish to find if there are a period of consecutive years such that total number of cars produced in this period is ex actly equal to M. That's, we wish to find if there exist I and J, 1⩽i⩽j⩽n 1 \leqslant i \leqslant j \leqslant N, such that∑jk=ia[k]=m \sum _{k=i}^ja[k]= M. Please design a O (n) time greedy algorithm to solve this problem. You are need to give a pseudo code.

My Answer:

Def greedy_equal (A, M):
    Pre, post = 0, 0
    s = a[0] While
    post < Len (a)-1:
        if s < m:
            post + = 1
  
   s + = A[post]
        elif s > M:
            S-= A[pre]
            pre + = 1
        else: Return
            Pre, post return
    None

A = [2, 3, 2, 9, 4, 8, 6, ten, 6, 3, 9]
M =
Print (greedy_equal (a,m))
  

2 in a given sequence, a[1], a[2], ..., a[n], a number may occur a multiple number of times. A number is called the dominating number if it occurs strictly more than times. For example, in the sequence, 2, 4, 5, 2, 2, 6, 2, the number 2 which occurs 4 (> 7/2) is the dominating number. A sequence may have no dominating number. You may compare two numbers, a[i] and a[j], 1≤i < J≤n, to the IF a[i] = A[j]. However, we assume this comparison does isn't tell which one is smaller in case they not are. (The comparison of numbers in a sequence tells only equal or not, not the size.) ) kind the comparisons among the numbers in array a and counting to determine if array A has a dominating nu Mber. You can expect to know which number are smaller or larger if the two numbers in comparison are not both from array A. (Not A sequence The comparison between the two numbers is known as size. ) Your algorithm must use divide and conquer method and has O (NLGN) complexity.

My Answer:

In fact, if there is such a dominating number in the sequence, then the figure is either domination on the left half, or the right half is dominating number, or both sides are dominating numbers.
As a result, there are four of situations that exist:
1. No dominating number was found on either side. The algorithm returns none at this time
2. The right half found dominating number, and the left half had no dominating number. Because dominating number requires more than n/2 times, and the right half of the element is N/2 (to simplify the problem, do not consider rounding, where the assumption that n is even), so the right half of the dominating number appears more than N/4. Therefore, we need to combine the number of the left half, in order with this domination numbers, and record the number of equal, if greater than N/2, then return the number, vice versa return none.
3. The left half found dominating number, and the right half had no dominating number. is the same as 2, at this point the left and right sides operate the opposite.
4. Dominating number was found on both sides of the two dominating numbers. If the condition is met, return. Otherwise, return none
The time complexity is calculated as: T (n) =2t (N/2) +2n t (n) =2t (N/2) +2n
The complexity is obtained according to the principal theorem: O (nlogn) O (NLOGN)

3* designs A greedy algorithm with an O (n) complexity to solve problem 2nd.

My Answer:

def get_most (A):
    candidate=a[0]
    count = 1 for

    item in A[1::

        If Item = = Candidate:
            count +=1
        else:
            count-=1
            if Count = = 0:
            candidate = Item
            count = 1 return

    candidate
def get_count (A): C12/>most = Get_most (a)

    count = 0 for
    item in A:
        if most = = Item:
            count = 1

    if Count > Len (a) /2: Return
        most, Count
    else: return
        None

4 Given A sequence of n real numbers stored in a array, a[1], a[2], ..., a[n]. We wish to find two numbers a[i] and a[j], where I < J, such that A[i]≤a[j] and their sum is the largest. That is,a[i] + a[j] = Max {A[u] + a[v] | 1≤u < V≤.N, A[u]≤a[v]}. Please type a divide-and-conquer algorithm to solve the above problem. Please analyze the complexity of your algorithm. The complexity of your algorithm must be O (NLGN) or better.

My Answer:

In fact, there are three possible scenarios for the solution:
1. A[i] and A[j] are respectively in the left and right halves of the array. In this case, we first use O (N/2) time to find the right half of the maximum value a[j], and then use O (N/2) time to find the left half than a[j] small maximum number a[i], and then return to A[I]+A[J] can be.
2. A[i],a[j] are on the left half. Returns the result of the left half at this time.
3. A[i],a[j] are on the right half. This returns the result of the right half.
Complexity Analysis:
T (n) =2t (N/2) +n
According to the principal theorem, the complexity of O (Nlogn) is known.

5 Design a O (n) greedy algorithm to solve problem 4.

My answer:

In fact, if (I,J) is solved, then a[i] must be the largest in a[1:i], otherwise, the maximum value in A[1:i is set to A[v] then the solution (V,J) is the better solution, the contradiction.
Similarly, a[j] must be the largest of the a[j:n.
Therefore, we use the two-cursor method, the time of O (n) from the right to the left and from the left to the right to find the maximum and the second largest value.

MAXJ = N for
(j = n-1 down to 1):
  if (A[j] > A[MAXJ]) Then:
    MAXJ = j
  Else:
    Check if (j, MAXJ) is A better solution

6 Given A sequence of n (n > 2) real numbers in a array, a[1], a[2], ..., a[n], we wish to find two numbers a[i] and A[J], where I < J, such that A[i]≤a[j] and their distance is the largest. That is,j-i = max {v-u | 1≤u < V≤n, A[u]≤a[v]}. If no such pair,-∞. Please type a O (NLGN) algorithm to solve this problem. Can use any method, including divide & Conquer, greedy, or dynamic programming.

my Answer

In fact, for the solution, (I,J), you can know that a[i] must be the smallest number in a[1:i], and a[j] must be the largest number in a[j:n.
Constructs a two array left and right, where left[i] represents the minimum value in a[1:i], Right[j] represents the maximum value in A[j:n.

def maxdist (A): left=[] right = [] Left.append (a[0)) Right.append (A[-1]) I in range (1,len (a)): Left.append (min (a[i], left[-1)) for I in Range (0,len (a) -1,-1): Right.append (ma X (A[i],right[-1])) right = Right.reverse () i = 0 j = Len (A)-1 dist =-1 while j > 0 and I < Len (  A): If Left[i] < Right[j]: dist = max (dist, j-i) J-= 1 else:i = 1 Return Dist 

7* A sequence of 2k numbers, A1, A2, ..., a2k, have k different values with each value occurs exactly. Now, we wish to find the K pairs such the two numbers in each pair the have value. For example, a sequence of 6 numbers are a1 = 5, a2 = 7.5, a3 = 7.5, A4 = ten, A5 = 5, A6 = 10. Then the answer should be (A1, A5), (A2, A3), (A4, A6). Prove that, in the "worst case", any algorithm needs at Least (KLGK) comparisons to find the K pairs. We assume there are 3 possible outcomes by comparing two A and B, which is, a = B, a < B, and a > B.

my Answer

For the worst case scenario, we can use the sort algorithm to sort, the minimum complexity of the sort algorithm is O (KLGK), and then iterate through it again with the need O (k), Total O (klgk).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.