-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhourglassSum.js
94 lines (76 loc) · 2.05 KB
/
hourglassSum.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Given a 6x6 2D Array:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in A to be a subset of values with indices falling in this pattern in arr's graphical representation:
a b c
d
e f g
There are 16 hourglasses in arr, and an hourglass sum is the sum of an hourglass' values. Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
For example, given the 2D array:
-9 - 9 - 9 1 1 1
0 - 9 0 4 3 2
- 9 - 9 - 9 1 2 3
0 0 8 6 6 0
0 0 0 - 2 0 0
0 0 1 2 4 0
We calculate the following 16 hourglass values:
-63, -34, -9, 12,
-10, 0, 28, 23,
-27, -11, -2, 10,
9, 17, 25, 18
Our highest hourglass value is 28 from the hourglass:
0 4 3
1
8 6 6
Function Description:
Complete the function hourglassSum in the editor below. It should return an integer, the maximum hourglass sum in the array.
*/
function isNotAnEdge(x, y, arr) {
if (y > 0 && y <= arr.length - 2) {
if (x > 0 && x <= arr[y].length - 2) {
return true;
}
}
return false;
}
function calcHourglassSumm (x,y,arr) {
let sum = 0;
sum += arr[y+1][x-1]; // top left
sum += arr[y+1][x]; // top middle
sum += arr[y+1][x+1]; // top right
sum += arr[y][x]; // center
sum += arr[y-1][x-1]; // bottom left
sum += arr[y-1][x]; // bottom middle
sum += arr[y-1][x+1]; // bottom right
return sum;
}
// O(n^2)
function hourglassSum(arr) {
let maxHourglassSum = -Infinity;
for (let y = 0; y < arr.length; y++) {
for (let x = 0; x < arr[y].length; x++) {
if (isNotAnEdge(x, y, arr)) {
const hourglassResult = calcHourglassSumm(x,y,arr);
if (hourglassResult > maxHourglassSum) {
maxHourglassSum = hourglassResult;
}
}
}
}
return maxHourglassSum;
}
// 28
const test1 = [
[-9, -9, -9, 1, 1, 1],
[0, -9, 0, 4, 3, 2],
[-9, -9, -9, 1, 2, 3],
[0, 0, 8, 6, 6, 0],
[0, 0, 0, -2, 0, 0],
[0, 0, 1, 2, 4, 0]
];
console.log('hourglassSum(test1)', hourglassSum(test1));