这是一个问题:codility.com/programmers/task/number_solitaire
和下面的链接是我的结果(来自Codility的50%):https ://codility.com/demo/results/training8AMJZH-RTA/
我的代码(最初,我尝试使用Kadane的Algo解决此问题):
class Solution {
public int solution(int[] A) {
int temp_max = Integer.MIN_VALUE;
int max = 0;
int k = 1;
if(A.length == 2) return A[0] + A[A.length-1];
for(int i = 1; i < A.length-1; i++) {
if(temp_max < A[i]) temp_max = A[i];
if(A[i] > 0) {
max += A[i];
temp_max = Integer.MIN_VALUE;
k = 0;
} else if(k % 6 == 0) {
max += temp_max;
temp_max = Integer.MIN_VALUE;
k = 0;
}
k++;
} …Run Code Online (Sandbox Code Playgroud)