-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum-grid.js
51 lines (46 loc) · 1.19 KB
/
maximum-grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @desc problem : 격자판 최대
* @desc site : Olympiad
* @desc level: 2
* @desc problem : 이중 순회, Math.max 함수를 통한 최대 값 추출
*/
/**
* Solution
* @param {array} grid: 5x5 Grid Array
*/
function solution(grid) {
const gridSize = grid.length;
let answer = 0;
let accRightDiag = 0;
let accLeftDiag = 0;
for (let i = 0; i < grid.length; i++) {
let accCol = 0;
let accRow = 0;
for (let j = 0; j < grid.length; j++) {
//좌측 대각선 합
if (i === j) {
accLeftDiag += grid[i][j];
}
//우측 대각선 합
if (gridSize - 1 - i === j) {
accRightDiag += grid[i][j];
}
//행, 열
accCol += grid[i][j];
accRow += grid[j][i];
}
const max = Math.max(accCol, accRow, accLeftDiag, accRightDiag);
if (max > answer) {
answer = max;
}
}
return answer;
}
const answer = solution([
[10, 13, 10, 12, 15],
[12, 39, 30, 23, 11],
[11, 25, 50, 53, 15],
[19, 27, 29, 37, 27],
[19, 13, 30, 13, 19]
]);
console.log(answer); //155 (10+39+50+37+19)