Google在Codility.com上的一个测试范例
2011-02-23 10:10
316 查看
Task description
Equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:
A[0]=-7 A[1]=1 A[2]=5 A[3]=2 A[4]=-4 A[5]=3 A[6]=0
3 is an equilibrium index, because:
A[0]+A[1]+A[2]=A[4]+A[5]+A[6]
6 is also an equilibrium index, because:
A[0]+A[1]+A[2]+A[3]+A[4]+A[5]=0
(sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence A.
If you still have doubts, this is a precise definition: the integer k is an equilibrium index of a sequence
if and only if
and
.
Assume the sum of zero elements is equal zero. Write a function
int equi(int[] A);
that given a sequence, returns its equilibrium index (any) or -1 if no equilibrium indexes exist. Assume that the sequence may be very long.
Copyright 2009-2010 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Analysis
Detected time complexity:
[b]O(n)
[/b]
Equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:
A[0]=-7 A[1]=1 A[2]=5 A[3]=2 A[4]=-4 A[5]=3 A[6]=0
3 is an equilibrium index, because:
A[0]+A[1]+A[2]=A[4]+A[5]+A[6]
6 is also an equilibrium index, because:
A[0]+A[1]+A[2]+A[3]+A[4]+A[5]=0
(sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence A.
If you still have doubts, this is a precise definition: the integer k is an equilibrium index of a sequence
if and only if
and
.
Assume the sum of zero elements is equal zero. Write a function
int equi(int[] A);
that given a sequence, returns its equilibrium index (any) or -1 if no equilibrium indexes exist. Assume that the sequence may be very long.
Copyright 2009-2010 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
int equi ( const vector<int> &A ) { // write your code here long long sum = 0; int len = A.size(); for(int i=0; i<len; ++i) { sum += A[i]; } if(len == 0) return -1; if(len == 1) return 0; long long lsum=0, rsum = sum; for(int i=0; i<len; ++i) { rsum -= A[i]; if(rsum == lsum) return i; lsum += A[i]; } return -1; }
Analysis
Detected time complexity:
[b]O(n)
test | time | result |
---|---|---|
example Test from the task description | 0.001 s. | OK |
extreme_empty Empty array | 0.004 s. | OK |
extreme_first | 0.004 s. | OK |
extreme_large_numbers Sequence with extremly large numbers testing arithmetic overflow. | 0.004 s. | OK |
extreme_last | 0.004 s. | OK |
extreme_single_zero | 0.004 s. | OK |
extreme_sum_0 sequence with sum=0 | 0.001 s. | OK |
simple | 0.001 s. | OK |
single_non_zero | 0.004 s. | OK |
combinations_of_two multiple runs, all combinations of {-1,0,1}^2 | 0.001 s. | OK |
combinations_of_three multiple runs, all combinations of {-1,0,1}^3 | 0.001 s. | OK |
small_pyramid | 0.004 s. | OK |
large_long_sequence_of_ones | 0.008 s. | OK |
large_long_sequence_of_minus_ones | 0.020 s. | OK |
medium_pyramid | 0.004 s. | OK |
large_pyramid Large performance test, O(n^2) solutions should fail. | 0.052 s. | OK |
相关文章推荐
- 用python开发了一个简单apache web服务端范例,在win10 + apache2.4.9 + python3.5 测试成功
- Google C++测试框架系列入门篇:第二章 开始一个新项目
- 一个困扰很久的异常—java.lang.NoClassDefFoundError: com/google/gson/Gson
- (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) (4)设计一个测试类
- 谷歌推出的基于WebGL下的鱼缸测试页面(用于网页速度测试)http://webglsamples.googlecode.com/hg/aquarium/aquarium.html
- Appium测试项目运行是报错“java.lang.NoClassDefFoundError: com/google/common/base/Function”
- // 2、写一个网址过滤的功能,要求得到此网址的域名,比如下面地址: //http://www.google.com/search?hl=en&source=hp&q=objective+c /
- 我在Google AppEngine上部署了一个Java应用(OpenID测试)
- 发现一个比较好的库: com.google.common.base
- 自己写的一个进程池的简单测试范例
- 【一个经典的google面试题】在浏览器中输入Google.com并且按下回车之后发生了什么?
- 【一个经典的google面试题】在浏览器中输入Google.com并且按下回车之后发生了什么?
- 和一个测试工程师的对话-From Google testing blog
- 一个神奇的网站http://www.googleisok.com/
- 今天看到了一个更新AndroidRom的好东西code_google_com
- goagent -- 进行测试开发前必须配置完的一个工具,能正常使用搜索引擎google
- Appium测试项目运行是报错“java.lang.NoClassDefFoundError: com/google/common/base/Function”
- 【软件测试】Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet
- 用python开发了一个简单apache web服务端范例,在win10 + apache2.4.9 + python3.5 测试成功
- 一个网页通用的测试用例(借鉴他人的保存,加注释)