Google在Codility.com上的一个测试范例

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$A[0],A[1],/ldots,A[n-1]$ if and only if $0 /leq k < n$ and$/sum_{i=0}^{k-1} A[i] = /sum_{i=k+1}^{n-1} A[i]$.

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:
O(n)
testtimeresult
example 
Test from the task description
0.001 s.OK
extreme_empty 
Empty array
0.004 s.OK
extreme_first0.004 s.OK
extreme_large_numbers 
Sequence with extremly large numbers testing arithmetic overflow.
0.004 s.OK
extreme_last0.004 s.OK
extreme_single_zero0.004 s.OK
extreme_sum_0 
sequence with sum=0
0.001 s.OK
simple0.001 s.OK
single_non_zero0.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_pyramid0.004 s.OK
large_long_sequence_of_ones0.008 s.OK
large_long_sequence_of_minus_ones0.020 s.OK
medium_pyramid0.004 s.OK
large_pyramid 
Large performance test, O(n^2) solutions should fail.
0.052 s.OK

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值