Codility 1: equilibrium

提交了格灵深瞳的简历后,收到需要先进行一个简单的技术测试的通知,临时抱佛脚,先刷刷上面几道题:

题目要求

A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e. 

A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

For example, consider the following array A consisting of N = 8 elements:

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

P = 1 is an equilibrium index of this array, because:

  • A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]

P = 3 is an equilibrium index of this array, because:

  • A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]

P = 7 is also an equilibrium index, because:

  • A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0

and there are no elements with indices greater than 7.

P = 8 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.

Write a function:

int solution(int A[], int N);

that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

For example, given array A shown above, the function may return 1, 3 or 7, as explained above.

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

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).

Elements of input arrays can be modified.

代码:

int solution(vector<int> &A){
    int sum = 0;
    for(int i =0;i<A.size();i++){
        sum = sum + A[i];
    }
    int for_sum = 0;
    int result = 0;
    for(int i =0;i<A.size();i++){
        sum = sum-A[i];
        if(sum == for_sum){
            result = i;
            break;
        }
        for_sum = for_sum + A[i];
    }
    return result;
}

  

转载于:https://www.cnblogs.com/xiamaogeng/p/4362951.html

Sequential Equilibrium(序惯均衡)是精炼纳什均衡的一种形式,它是由不包含不可置信行动的战略所组成的纳什均衡。在序惯均衡中,构成该均衡的战略在每个决策点上都是最优的。因此,序惯均衡也被称为"精炼纳什均衡"或"序惯均衡"。需要注意的是,并非所有的纳什均衡都是合理的,只有那些战略不包含不可置信行动的纳什均衡才是合理的。不可置信威胁是指由于事前最优和事后最优不同而导致的许多帕累托效率无法实现的情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [博弈论笔记:动态博弈](https://blog.csdn.net/qq_40206371/article/details/117626623)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [机器学习(ML)、深度学习(DL)和图像处理(opencv)专用英语词典](https://blog.csdn.net/wyx100/article/details/74635853)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值