Lesson 7: MaxSliceSum
https://codility.com/programmers/lessons/7
This is another max slice problem.
The difference from the original cordiality reading material Open reading material (PDF) is that the slice can not be empty.
So if the max value of the slice that can end at the current position is smaller than the value at the current position, we take the later. (note that in the original version in the open reading material uses '0' instead, as the slice can be empty).
This solution gives the 100% score.
int solution(int A[], int N)
{
int max_ending = A[0];
int max_slice = A[0];
int i;
for (i = 1; i < N; i++){
int tmp = max_ending + A[i];
max_ending = tmp > A[i] ? tmp : A[i];
max_slice = max_slice < max_ending ? max_ending : max_slice;
}
return max_slice;
}
このコメントは投稿者によって削除されました。
返信削除