-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-grid.js
More file actions
51 lines (46 loc) ยท 1.19 KB
/
maximum-grid.js
File metadata and controls
51 lines (46 loc) ยท 1.19 KB
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)