一道笔试题 体现出的严谨和算法
2010-11-24 11:04
176 查看
一开始用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.
nalysis
Detected time complexity:
O(n)
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)
test | time | result |
---|---|---|
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 |
相关文章推荐
- 一道笔试题 体现出的严谨和算法
- 一道微软笔试题,算法才是王道
- 一道算法笔试题
- java实现堆排序(2016年腾讯内推笔试的一道算法题)
- 一道有道实习生笔试算法题分析
- 一道求质数的题,不同的算法体现不同的程序性能。
- 每日一道算法题:Google 2009年某笔试题:求出元素位于0到9之间的集合A中大于某个给定正整数K的组成的最小正整数。
- 洗牌算法与tencent的一道笔试题
- 【算法系列】一道笔试试题——回文数算法
- 今天笔试的一道算法题,每次都吃后悔药,痛苦!
- 数字字符串一道有道实习生笔试算法题分析
- 炒冷饭 《从一道笔试题谈算法优化》全文【作者: 恋花蝶】
- 一道简单又不简单的算法笔试题
- 月初新浪的一道关于算法的笔试题
- 阿里巴巴一道笔试算法题分析…
- 月初新浪的一道关于算法的笔试题
- 黑马程序员--舍友笔试中的一道算法题
- 精妙算法收集---一道有趣的腾讯笔试加分题
- 一道求职笔试题:c#算法题