즉, 행과열을 각각 한칸씩 이동하며 해당 모래시계 배열에 해당하는 인덱스에 위치한 값을 추출
그림 2. 3x3 행렬에서의 모래시계 배열 위치
추출한 배열값의 합을 비교하여 최댓값을 추출
// 생략
/*
* Complete the 'hourglassSum' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
function hourglassSum(arr) {
// Write your code here
const len = arr.length;
const lines = [...arr.join(',').split(',')];
let max = -Infinity;
for(let j = 0; j<len-2;j++) {
for(let i = 0; i <len-2;i++){
const j1 = j*len;
const j2 = (j+1)*len;
const j3 = (j+2)*len;
const hourglass = [lines[i+j1], lines[i+1+j1], lines[i+2+j1], lines[i+1+j2], lines[i+j3], byte[i+1+j3], byte[i+2+j3]];
const sum = hourglass.reduce((a, b) => a += +b, 0);
max = Math.max(max, sum);
}
}
return max;
}
// ...후략