codility: Dominator, EquiLeader

Task description

A zero-indexed array A consisting of N integers is given. The dominatorof 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

int solution(const vector<int> &A);

that, given a zero-indexed 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.

Assume that:

  • 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].

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.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

就是找一个数组的众数(出现次数大于N/2)。

主要利用这样一个特性:每次删掉不相同的两个元素,因为删掉两个不同的元素之后,数组里剩下的元素的众数还是原数组的众数。(The leader in the new sequence occurs more than n/2 − 1 = (n−2)/times )

有了上面的特性那程序就很好实现了:

// you can also use includes, for example:
// #include <algorithm>
#include <stack>
int solution(const vector<int> &A) {
    // write your code in C++98
    stack<int> sstack;
    for(int i=0;i<A.size();i++) {
        if(sstack.empty()||A[i]==A[sstack.top()]) {
            sstack.push(i);
        }else {
            sstack.pop();
        }
    }
    if(sstack.empty())
        return -1;
    else {
        int candidate = sstack.top();
        int count = 0;
        for(int i=0;i<A.size();i++) {
            if(A[i]==A[candidate]) {
                count++;
            }
        }
        if(count>A.size()/2) {
            return candidate;
        }else {
            return -1;
        }
    }
}

其实用栈有点浪费空间,因为栈里的元素肯定是一样的,所以只需要两个变量分别记录下栈的大小以及栈内元素的值就好。

需要注意的是最后需要检查一下栈顶的那个元素到底是不是众数。(因为比方说数组是1,2,3. 最后栈里留下的3显然不是众数)。

Leader这个lesson还有一个题是EquiLeader:

Task description

A non-empty zero-indexed 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:

int solution(vector<int> &A);

that, given a non-empty zero-indexed 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.

Assume that:

  • 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].

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

还是个找众数的题,只是这一次需要完成的目标是:找出一个数组有多少个分割点,这个分割点使得左右两边的子数组的众数相同。

显然左右两边的子数组如果他们有相同的众数的话,那这个众数肯定是原数组的众数。

所以可以先找出原来的众数,然后重新遍历,判断当前的位置是不是一个有效的分割点就好。

// you can also use includes, for example:
// #include <algorithm>
#include <stack>
int solution(vector<int> &A) {
    // write your code in C++98
    stack<int> sstack;
    int count = 0;
    int res = 0;
    for(int i=0;i<A.size();i++) {
        if(sstack.empty()) {
            sstack.push(A[i]);
        }else {
            if(sstack.top()==A[i]) {
                sstack.push(A[i]);
            }else {
                sstack.pop();
            }
        }
    }
    if(sstack.empty()) {
        return 0;
    }else {
        int candidate = sstack.top();
        for(int i=0;i<A.size();i++) {
            if(A[i]==candidate)
                count++;
        }
        if(count>A.size()/2) {
            int leftCount = 0;
            for(int i=0;i<A.size();i++) {
                if(A[i]==candidate) {
                    leftCount++;
                }
                if(leftCount>(i+1)/2 && (count-leftCount)>(A.size()-i-1)/2) {
                    res++;
                }
            }
            return res;
        }
        return 0;
    }
}

 

转载于:https://www.cnblogs.com/parapax/p/3642769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值