Tasks Details
easy
1.
MaxSliceSum
Find a maximum sum of a compact subsequence of array elements.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns the maximum sum of any slice of A.
For example, given array A such that:
A[0] = 3 A[1] = 2 A[2] = -6 A[3] = 4 A[4] = 0the function should return 5 because:
- (3, 4) is a slice of A that has sum 4,
- (2, 2) is a slice of A that has sum −6,
- (0, 1) is a slice of A that has sum 5,
- no other slice of A has sum greater than (0, 1).
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..1,000,000];
- each element of array A is an integer within the range [−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 Java 8
Time spent on task 1 minutes
Notes
not defined yet
Task timeline
Code: 13:26:33 UTC,
java,
autosave
Code: 13:26:42 UTC,
java,
verify,
result: Failed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static int solution(int[] A) {
// write your code in Java SE 8
long maxEnding = 0;
long maxSlice = Integer.MIN_VALUE;
long maxValue = Integer.MIN_VALUE;
for (int a : A) {
// a> 0이면서 maxEnding or maxSlice 0보다 작다는건 음수값에서 양수값인 값이 나왔을때
if ((maxEnding < 0 || maxSlice < 0) && a > 0) {
maxValue = maxSlice;
maxSlice = 0;
maxEnding = 0;
}
maxEnding = Math.max(Integer.MIN_VALUE, maxEnding + a);
maxSlice = Math.max(maxSlice, maxEnding);
if (a > maxValue) {
maxValue = a;
}
if (maxSlice > maxValue) {
maxValue = maxSlice;
}
}
return (maxValue > maxSlice) ? (int)(maxValue) : (int)(maxSlice);
}
출처: https://namget.tistory.com/entry/코딜리티-Lesson-9MaxSliceSum [남갯,YTS의 개발,일상블로그]
}
Analysis
Compile error
Solution.java:34: error: <identifier> expected ??: https://namget.tistory.com/entry/????-Lesson-9MaxSliceSum [??,YTS? ??,?????] ^ Solution.java:34: error: <identifier> expected ??: https://namget.tistory.com/entry/????-Lesson-9MaxSliceSum [??,YTS? ??,?????] ^ 2 errors
Code: 13:26:46 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static int solution(int[] A) {
// write your code in Java SE 8
long maxEnding = 0;
long maxSlice = Integer.MIN_VALUE;
long maxValue = Integer.MIN_VALUE;
for (int a : A) {
// a> 0이면서 maxEnding or maxSlice 0보다 작다는건 음수값에서 양수값인 값이 나왔을때
if ((maxEnding < 0 || maxSlice < 0) && a > 0) {
maxValue = maxSlice;
maxSlice = 0;
maxEnding = 0;
}
maxEnding = Math.max(Integer.MIN_VALUE, maxEnding + a);
maxSlice = Math.max(maxSlice, maxEnding);
if (a > maxValue) {
maxValue = a;
}
if (maxSlice > maxValue) {
maxValue = maxSlice;
}
}
return (maxValue > maxSlice) ? (int)(maxValue) : (int)(maxSlice);
}
}
Analysis
Code: 13:26:51 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static int solution(int[] A) {
// write your code in Java SE 8
long maxEnding = 0;
long maxSlice = Integer.MIN_VALUE;
long maxValue = Integer.MIN_VALUE;
for (int a : A) {
// a> 0이면서 maxEnding or maxSlice 0보다 작다는건 음수값에서 양수값인 값이 나왔을때
if ((maxEnding < 0 || maxSlice < 0) && a > 0) {
maxValue = maxSlice;
maxSlice = 0;
maxEnding = 0;
}
maxEnding = Math.max(Integer.MIN_VALUE, maxEnding + a);
maxSlice = Math.max(maxSlice, maxEnding);
if (a > maxValue) {
maxValue = a;
}
if (maxSlice > maxValue) {
maxValue = maxSlice;
}
}
return (maxValue > maxSlice) ? (int)(maxValue) : (int)(maxSlice);
}
}
Analysis
Code: 13:26:55 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static int solution(int[] A) {
// write your code in Java SE 8
long maxEnding = 0;
long maxSlice = Integer.MIN_VALUE;
long maxValue = Integer.MIN_VALUE;
for (int a : A) {
// a> 0이면서 maxEnding or maxSlice 0보다 작다는건 음수값에서 양수값인 값이 나왔을때
if ((maxEnding < 0 || maxSlice < 0) && a > 0) {
maxValue = maxSlice;
maxSlice = 0;
maxEnding = 0;
}
maxEnding = Math.max(Integer.MIN_VALUE, maxEnding + a);
maxSlice = Math.max(maxSlice, maxEnding);
if (a > maxValue) {
maxValue = a;
}
if (maxSlice > maxValue) {
maxValue = maxSlice;
}
}
return (maxValue > maxSlice) ? (int)(maxValue) : (int)(maxSlice);
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
7.
0.004 s
OK
8.
0.004 s
OK
9.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
7.
0.004 s
OK
8.
0.004 s
OK
9.
0.004 s
OK
10.
0.004 s
OK
11.
0.008 s
OK
12.
0.004 s
OK
13.
0.004 s
OK
14.
0.004 s
OK
15.
0.004 s
OK
16.
0.004 s
OK
17.
0.004 s
OK
18.
0.004 s
OK
19.
0.004 s
OK
20.
0.004 s
OK
21.
0.008 s
OK
22.
0.004 s
OK
23.
0.004 s
OK
24.
0.004 s
OK
25.
0.004 s
OK
26.
0.004 s
OK
27.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.004 s
OK
1.
0.008 s
OK
1.
0.004 s
OK