less than 1 minute read

[Lesson9] Maximum slice problem

MaxDoubleSliceSum

import java.util.*;

class Solution {
    public int solution(int[] A) {
        int len = A.length;
        int[] a1 = new int[len];
        int[] a2 = new int[len];
        int max = 0;
        for(int i = 1; i < len-1; i++){
            a1[i] = Math.max(a1[i-1] + A[i], 0);
        }
        for(int i = len-2; i > 0; i--){
            a2[i] = Math.max(a2[i+1] + A[i], 0);    
        }
        for(int i = 1; i < len-1; i++){
            max = Math.max(max, a1[i-1] + a2[i+1]);    
        }
        return max;
    }
}s