272. MaxDoubleSliceSum

아현·2021년 8월 16일
0

Algorithm

목록 보기
284/400

1. JavaScript



// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');

function solution(A) {
    const N = A.length;

    if(N === 3) return 0;

    let start = Array(N).fill(0);
    let tail = Array(N).fill(0);

    for(let i = 1; i < N-1; i++) {
        start[i] = Math.max(0, start[i-1] + A[i]);
    }

    for(let i = N-2; i >= 1; i--) {
        tail[i] = Math.max(0, tail[i+1] + A[i]);
    }

    let max = 0;

    for(let i = 1; i < N-1; i++) {
        max = Math.max(max, start[i-1] + tail[i+1]);    
    } 

    return max;
}
 



2. Python



# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    length = len(A)
    start = [0] * length
    end = [0] * length

    for i in range(1, length - 1):
        start[i] = max(0, start[i-1] + A[i])
    
    for i in range(length - 2, 0, -1):
        end[i] = max(0, end[i + 1] + A[i])

    answer = 0
    for i in range(1, length - 1):
        answer  = max(answer, start[i-1] + end[i+1])

    return answer

profile
For the sake of someone who studies computer science

0개의 댓글