Tasks Details
easy
1.
MaxSliceSum
Find a maximum sum of a compact subsequence of array elements.
Task Score
100%
Correctness
100%
Performance
100%
请写一个函数
function solution(A);
对于给定的序列 S, 返回它的最大子序列之和。
一个序列S的子序列是指在该序列S中拥有连续的数组下标的元素所组成的序列。
例如, 对于给定的序列
A[0] = 3 A[1] = 2 A[2] = -6 A[3] = 4 A[4] = 0我们可以得到下面的子序列:
[2,−6,4,0] [3,2,−6,4,0] [2] [3,2,−6] [−6,4] [](空的子序列)
以及其他的子序列. 子序列 [] 被称为空的子序列, 因为其中不包含任何的元素。 下面的序列则不是给定序列的子序列S:
[3,−6,0], [1], [3,2,−6,0]
最大的子序列之和是指一个序列中所有非空的子序列的元素的总和的最大值. 用更精确的方式来表示:
max(S[p] + S[p+1] + ... + S[q] {其中p, q都是整数 并且} p<q p<q)
对于上面的序列 S, 最大的子序列之和为
5 = S[0] + S[1]
假定:
- N 是 [1..1,000,000] 内的 整数;
- 数组 A 每个元素是取值范围 [−1,000,000..1,000,000] 内的 整数 ;
- the result will be an integer within the range [−2,147,483,648..2,147,483,647].
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 7 minutes
Notes
not defined yet
Task timeline
Code: 05:46:01 UTC,
java,
autosave
Code: 05:49:14 UTC,
js,
autosave
Code: 05:49:43 UTC,
js,
autosave
Code: 05:49:58 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(cur_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, max_sum)
}
}
Code: 05:50:08 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(cur_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
}
Code: 05:50:22 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(cur_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
return max_sum
}
Code: 05:50:31 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(cur_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
return max_sum
}
Analysis
expand all
Example tests
1.
0.056 s
WRONG ANSWER,
got 9 expected 5
Code: 05:50:53 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(cur_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
return max_sum
}
Code: 05:51:04 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(cur_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
console.log(cur_sum, max_sum)
}
return max_sum
}
Analysis
expand all
Example tests
1.
0.060 s
WRONG ANSWER,
got 9 expected 5
stdout:
5 5 5 5 9 9 9 9
Code: 05:51:34 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(max_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
console.log(cur_sum, max_sum)
}
return max_sum
}
Code: 05:51:36 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(max_sum, cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
console.log(cur_sum, max_sum)
}
return max_sum
}
Analysis
expand all
Example tests
1.
0.056 s
WRONG ANSWER,
got 9 expected 5
stdout:
5 5 5 5 9 9 9 9
Code: 05:51:48 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(A[i], cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
console.log(cur_sum, max_sum)
}
return max_sum
}
Analysis
Code: 05:52:20 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(A[i], cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
return max_sum
}
Code: 05:52:25 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(A[i], cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
return max_sum
}
Analysis
Code: 05:52:29 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
if(A.length == 0) return 0
var cur_sum = A[0]
var max_sum = A[0]
for(var i=1;i<A.length;i++){
cur_sum = Math.max(A[i], cur_sum+A[i])
max_sum = Math.max(max_sum, cur_sum)
}
return max_sum
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.056 s
OK
2.
0.056 s
OK
3.
0.056 s
OK
1.
0.056 s
OK
2.
0.056 s
OK
3.
0.052 s
OK
4.
0.056 s
OK
5.
0.056 s
OK
6.
0.056 s
OK
7.
0.060 s
OK
8.
0.056 s
OK
9.
0.056 s
OK
1.
0.056 s
OK
2.
0.056 s
OK
3.
0.052 s
OK
4.
0.056 s
OK
5.
0.056 s
OK
6.
0.056 s
OK
7.
0.056 s
OK
8.
0.056 s
OK
9.
0.056 s
OK
10.
0.056 s
OK
11.
0.056 s
OK
12.
0.056 s
OK
13.
0.056 s
OK
14.
0.056 s
OK
15.
0.056 s
OK
16.
0.056 s
OK
17.
0.056 s
OK
18.
0.056 s
OK
19.
0.056 s
OK
20.
0.056 s
OK
21.
0.052 s
OK
22.
0.056 s
OK
23.
0.056 s
OK
24.
0.056 s
OK
25.
0.056 s
OK
26.
0.056 s
OK
27.
0.056 s
OK
1.
0.056 s
OK
1.
0.052 s
OK
1.
0.056 s
OK
1.
0.056 s
OK
1.
0.056 s
OK