[Codility]Lesson8

Posted ViviranZ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codility]Lesson8相关的知识,希望对你有一定的参考价值。

Task1

An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A.

For example, consider array A such that

A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3

The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.

Write a function

def solution(A)

that, given an array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator.

For example, given array A such that

A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3

the function may return 0, 2, 4, 6 or 7, as explained above.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    N=len(A)
#     candi=A[0]
#     size=1
    size=0
    for i in range(N):
        if size==0:
            #print("A")
            candi=A[i]
            size=1
        elif A[i]==candi:
            #print("B")
            size=size+1
        else:#A[i]!=candi
            #print("C")
            size=size-1
        #print("when i=",i,"candi=",candi,"size=",size)
    num=0
    for i in range(N):
        if A[i]==candi:
            num=num+1
            result=i
    if num>N//2:
        return result
    else:
        return -1

 Task2:

A non-empty array A consisting of N integers is given.

The leader of this array is the value that occurs in more than half of the elements of A.

An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N − 1] have leaders of the same value.

For example, given array A such that:

A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2

we can find two equi leaders:

  • 0, because sequences: (4) and (3, 4, 4, 4, 2) have the same leader, whose value is 4.
  • 2, because sequences: (4, 3, 4) and (4, 4, 2) have the same leader, whose value is 4.

The goal is to count the number of equi leaders.

Write a function:

def solution(A)

that, given a non-empty array A consisting of N integers, returns the number of equi leaders.

For example, given:

A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2

the function should return 2, as explained above.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    pass
    N=len(A)
    size=0
    for i in range(N):
        if size==0:
            candi=A[i]
            size=1
        elif A[i]==candi:
            size=size+1
        else:
            size=size-1
    Num=0
    B=[]
    for i in range(N):
        if A[i]==candi:
            Num=Num+1
        B.append(Num)#B[i]存储从0到i的数列里candi的个数
    if Num<=N/2:#当N even :一半及以下;当N odd:(N-1)/2以下
        return 0
    num=0
    for i in range(N):
        if B[i]>(i+1)/2 and B[i]<Num-(N-i-1)/2:
            num=num+1
    return num

 

以上是关于[Codility]Lesson8的主要内容,如果未能解决你的问题,请参考以下文章

html http://jsfiddle.net/lesson8/HkEuf/1/

斯坦福《机器学习》Lesson8感想-------1SMO

关于 Codility 的 HilbertMaze 任务

Codility 任务 TapEquilibrium 正确性仅为 33%

Solution to Triangle by Codility

CountNonDivisible - Codility 训练任务