272. MaxDoubleSliceSum
10727 ワード
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
Reference
この問題について(272. MaxDoubleSliceSum), 我々は、より多くの情報をここで見つけました https://velog.io/@corone_hi/272.-MaxDoubleSliceSumテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol