您的位置:首页 > 其它

一道笔试题 体现出的严谨和算法

2010-11-24 11:04 113 查看
一开始用Integer 怎么都超时,发现用int 才能过。

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.

01.

public

boolean

overflag=

false

;


02.


public

int

sum(

int

a,

int

b)


03.


{


04.


int

ret=a+b;


05.


if

(((a>

0

)&&(b>

0

))&&(ret<

0

))overflag =

true

;


06.


if

(((a<

0

)&&(b<

0

))&&(ret>

0

))overflag =

true

;


07.


return

ret;


08.


}


09.


public

int

equi (

int

[] A ){


10.


if

(A.length==

0

)

return

-

1

;


11.


if

(A.length==

1

)

return

0

;


12.


if

(A.length==

2

)

return

-

1

;


13.


int

j=

0

;


14.


int

right=

0

;


15.


int

left=

0

;


16.


int

i=

0

;


17.


for

(i=A.length-

1

;i>

0

;i--)


18.


{


19.


right=sum(right,A[i]);


20.


if

(overflag)

break

;


21.


}


22.



23.


if

((right==

0

)&&(i==

0

))

return

0

;


24.



25.


for

(j=

0

;j<i;j++)


26.


{


27.


left=sum(left,A[j]);


28.


if

(overflag)

return

-

1

;


29.


}


30.


if

(left==right)

return

0

;


31.


j=

0

;


32.


while

(j<A.length-

1

)


33.


{


34.


if

(left==right)

return

j;


35.


left=sum(left,A[j]);


36.


if

(overflag)

return

-

1

;


37.


right=sum(right,-A[j+

1

]);


38.


if

(overflag)

return

-

1

;


39.


j++;


40.


}


41.



42.


if

(left==right)

return

j;


43.


return

-

1

;


44.



45.


}


nalysis

Detected time complexity:

O(n)

testtimeresult
example

Test from the task description

0.524 s.OK

extreme_empty

Empty array

0.784 s.OK

extreme_first

0.572 s.OK

extreme_large_numbers

Sequence with extremly large numbers testing arithmetic overflow.

0.540 s.OK

extreme_last

0.588 s.OK

extreme_single_zero

0.504 s.OK

extreme_sum_0

sequence with sum=0

0.684 s.OK

simple

0.516 s.OK

single_non_zero

0.756 s.OK

combinations_of_two

multiple runs, all combinations of {-1,0,1}^2

0.516 s.WRONG ANSWER

got -1, but equilibrium point exists, for example on position 0

combinations_of_three

multiple runs, all combinations of {-1,0,1}^3

1.112 s.OK

small_pyramid

1.020 s.OK

large_long_sequence_of_ones

0.552 s.OK

large_long_sequence_of_minus_ones

0.536 s.OK

medium_pyramid

0.808 s.OK

large_pyramid

Large performance test, O(n^2)solutions should fail.

0.716 s.OK
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: