Skip to content

Commit 56c07f9

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅.
1 parent 9f62e7d commit 56c07f9

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { hourglassSum } from './2d_array';
5+
import TEST_CASES from './2d_array.testcases_test.json';
6+
7+
describe('arrays: 2d Array hourglassSum', () => {
8+
it('hourglassSum Test Cases', () => {
9+
expect.assertions(1);
10+
11+
TEST_CASES.forEach((test) => {
12+
const answer = hourglassSum(test.input);
13+
14+
console.debug(
15+
`gethourGlass(${test.input.toString()}) solution found: ${answer}`
16+
);
17+
18+
expect(answer).toStrictEqual(test.expected);
19+
});
20+
});
21+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"input": [
5+
[1, 1, 1, 0, 0, 0],
6+
[0, 1, 0, 0, 0, 0],
7+
[1, 1, 1, 0, 0, 0],
8+
[0, 0, 2, 4, 4, 0],
9+
[0, 0, 0, 2, 0, 0],
10+
[0, 0, 1, 2, 4, 0]
11+
],
12+
"expected": 19
13+
}
14+
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
export function gethourGlass(
2+
arr: number[][],
3+
positionX: number,
4+
positionY: number
5+
): number[] {
6+
const result: number[] = [];
7+
8+
// top
9+
result.push(arr[positionX - 1][positionY - 1]);
10+
result.push(arr[positionX - 1][positionY]);
11+
result.push(arr[positionX - 1][positionY + 1]);
12+
// middle
13+
result.push(arr[positionX][positionY]);
14+
// bottom
15+
result.push(arr[positionX + 1][positionY - 1]);
16+
result.push(arr[positionX + 1][positionY]);
17+
result.push(arr[positionX + 1][positionY + 1]);
18+
return result;
19+
}
20+
21+
export function hourglassSum(arr: number[][]): number | null {
22+
let matrixSize = 0;
23+
24+
if (arr?.[0]) {
25+
matrixSize = arr.length;
26+
}
27+
28+
const matrixStartIndex = 1;
29+
const matrixStopIndex = matrixSize - 2;
30+
31+
console.debug(`matrix size ${matrixSize}`);
32+
33+
let maxHourglassSum: number | null = null;
34+
35+
// recorrido
36+
for (let i = matrixStartIndex; i <= matrixStopIndex; i++) {
37+
for (let j = matrixStartIndex; j <= matrixStopIndex; j++) {
38+
// hourglass centers
39+
console.debug(`posicion (${i},${j}): ${arr[i][j]}`);
40+
41+
const houglassValues: number[] = gethourGlass(arr, i, j);
42+
43+
const thisHourglassSum = houglassValues.reduce(
44+
(a: number, b: number): number => a + b,
45+
0
46+
);
47+
48+
console.debug(houglassValues, `thisHourglassSum: ${thisHourglassSum}`);
49+
50+
if (
51+
maxHourglassSum === undefined ||
52+
maxHourglassSum === null ||
53+
thisHourglassSum > maxHourglassSum
54+
) {
55+
maxHourglassSum = thisHourglassSum;
56+
}
57+
}
58+
}
59+
60+
return maxHourglassSum;
61+
}
62+
63+
export default { hourglassSum };

0 commit comments

Comments
 (0)