//lesson answer in python
http://codesays.com/solutions-to-training-by-codility/
1、Prefix Sums
该做法通常从后往前循环。例如pass car
//alg study
https://class.coursera.org/algs4partI-004/
//算法介绍文档
https://codility.com/media/train/4-Sorting.pdf
//详细题目介绍
http://blog.csdn.net/sunbaigui/article/category/1769905/1
http://blog.csdn.net/caopengcs/article/category/1502799
//老外的codility代码,python
https://github.com/bluemihai/my-codility-solutions
https://github.com/zjsxzy/Codility
//中国的介绍和详解
http://www.cnblogs.com/parapax/tag/codility/
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
*****//好像是codility公司员工在github上的代码,最全的,连pdf都有
https://github.com/kevinmore/Codility
==========================
QSORT
- int compi(const void *a, const void *b)
- {
- const int *p = a;
- const int *q = b;
- return *p - *q;
- }
- int compd(const void *a, const void *b)
- {
- const int *p = a;
- const int *q = b;
- return (*p - *q) * (-1);
- }
- int main()
- {
- int a[1000];
- int i, len, type;
- while(scanf("%d %d", &len, &type) != EOF)
- {
- for(i = 0; i < len; i ++)
- {
- scanf("%d", &a[i]);
- }
- switch(type)
- {
- case 1 :
- //递增排序
- qsort(a, len, sizeof(a[0]), compi);
- break;
- case 2 :
- //递减排序
- qsort(a, len, sizeof(a[0]), compd);
- break;
- }
- if(type == 1)
- {
- printf("递增排序结果:\n");
- }else
- {
- printf("递减排序结果:\n");
- }
- for(i = 0; i < len; i ++)
- {
- printf("%d ", a[i]);
- }
- printf("\n");
- }
- return 0;
- }
=====================================================
搜索某个区间的和的最大值
1
2
3
4
5
6
7
8
9
|
def
solution
(
A
)
:
max_slice_ending_here
=
A
[
0
]
max_slice
=
A
[
0
]
for
element
in
A
[
1
:
]
:
max_slice_ending_here
=
max
(
element
,
max_slice_ending_here
+
element
)
max_slice
=
max
(
max_slice
,
max_slice_ending_here
)
return
max_slice
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def
solution
(
A
)
:
days
=
len
(
A
)
# If the number of days is zero or one, there
# is no time to get profit.
if
days
<
2
:
return
0
max_price_from_here
=
A
[
days
-
1
]
max_profit
=
0
for
index
in
xrange
(
days
-
2
,
-
1
,
-
1
)
:
# max_price_from_here-A[index] means the maximum
# profit from current day to end.
max_profit
=
max
(
max_profit
,
max_price_from_here
-
A
[
index
]
)
max_price_from_here
=
max
(
A
[
index
]
,
max_price_from_here
)
return
max_profit
|
如果是取减法的最大值,则是反序遍历
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
def
solution
(
A
)
:
days
=
len
(
A
)
# If the number of days is zero or one, there
# is no time to get profit.
if
days
<
2
:
return
0
max_price_from_here
=
A
[
days
-
1
]
max_profit
=
0
for
index
in
xrange
(
days
-
2
,
-
1
,
-
1
)
:
# max_price_from_here-A[index] means the maximum
# profit from current day to end.
max_profit
=
max
(
max_profit
,
max_price_from_here
-
A
[
index
]
)
max_price_from_here
=
max
(
A
[
index
]
,
max_price_from_here
)
return
max_profit
|
===================================================
欧几里得算法,求最大gcd
When we met with an empty wrapper, we must have been this position for twice. We use i for the first time and j for the second time. Due to the modulo feature, there must be nature number, to say k, so that: i * M + k * N = j * M. Then we could easily prove that the smallest (earliest) i must be zero (for all i != 0, then (i-i) * M + k * N = (j-i) * M ). So the first eaten position would be first position that you meet again. Finally, the j would be the number of chocolates that you will eat.
====================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def
solution
(
A
,
B
)
:
limit
=
len
(
A
)
# The possible largest N rungs
result
=
[
0
]
*
len
(
A
)
# The result for each query
B
=
[
(
1
<<
item
)
-
1
for
item
in
B
]
# Pre-compute B for optimization
# Compute the Fibonacci numbers for later use
fib
=
[
0
]
*
(
limit
+
2
)
fib
[
1
]
=
1
for
i
in
xrange
(
2
,
limit
+
2
)
:
fib
[
i
]
=
fib
[
i
-
1
]
+
fib
[
i
-
2
]
for
i
in
xrange
(
limit
)
:
# To climb to A[i] rungs, you could either
# come from A[i]-1 with an one-step jump
# OR come from A[i]-2 with a two-step jump
# So from A[i] rungs, the number of different ways of climbing
# to the top of the ladder is the Fibonacci number at position
# A[i] + 1
result
[
i
]
=
fib
[
A
[
i
]
+
1
]
&
B
[
i
]
return
result
|
=============================================================
Binary search algorithm //二分查找算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include <cmath>
int
TestLargeSum
(
const
std
::
vector
<
int
>
&A
,
int
K
,
int
LargeSum
)
{
int
i
,
n
,
j
;
n
=
A
.
size
(
)
;
long
long
sum
;
sum
=
0
;
j
=
0
;
for
(
i
=
0
;
i
<
n
;
i
++
)
{
sum
+
=
A
[
i
]
;
if
(
sum
>
LargeSum
)
{
j
++
;
if
(
j
>
(
K
-
1
)
)
return
0
;
sum
=
A
[
i
]
;
}
}
return
1
;
}
int
solution
(
int
K
,
vector
<
int
>
&A
)
{
int
i
,
n
,
Amax
,
LargeSum
;
int
r
,
b
,
e
,
LargeSumR
;
n
=
A
.
size
(
)
;
LargeSum
=
0
;
for
(
i
=
K
-
1
;
i
<
n
;
i
++
)
LargeSum
+
=
A
[
i
]
;
Amax
=
0
;
for
(
i
=
0
;
i
<
n
;
i
++
)
if
(
A
[
i
]
>
Amax
)
Amax
=
A
[
i
]
;
if
(
K
==
n
)
return
Amax
;
LargeSum
=
max
(
Amax
,
LargeSum
)
;
b
=
Amax
;
e
=
LargeSum
;
if
(
e
<
b
)
swap
(
e
,
b
)
;
LargeSumR
=
e
;
// BinSearch
while
(
b
<=
e
)
{
LargeSum
=
(
b
+
e
)
/
2
;
r
=
TestLargeSum
(
A
,
K
,
LargeSum
)
;
if
(
r
)
{
e
=
LargeSum
-
1
;
LargeSumR
=
LargeSum
;
}
else
{
b
=
LargeSum
+
1
;
}
}
return
LargeSumR
;
}
|
前几天复习了一下对分查找(Binary Search),它提供了在 O(log N) 时间内的 Find (查找操作),先来看看对分查找的叙述要求:
给定一个整数 X 和整数 ,后者已经预先排序,并且已经在内存中,求使得 的下标 i ,如果 X 不在数据之中,则返回 i = -1。
来看看实现源码:
int
BinarySearch(
const
int
A[],
int
X,
int
N)
{
int
Low, Mid, High;
Low = 0; High = N - 1;
while
(Low <= High)
{
Mid = (Low + High) / 2;
if
(A[Mid] < X)
Low = Mid + 1;
else
if
(A[Mid] > X)
High = Mid - 1;
else
return
Mid;
/*Found*/
}
return
-1;
/*Not Found: Return -1*/
}
|
这里要注意的是:作为参数的数组 A,在调用函数 BinarySearch 的时候,已经经过了一次排序。