From 17e6592fbd313ca344e715b93181a74d4c58bf22 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 17 Jan 2025 19:49:32 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Array:=202D=20Array=20-=20DS.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arrays/2d_array.md | 135 ++++++++++++++++++ .../interview_preparation_kit/arrays/crush.md | 8 +- .../arrays/2d_array.test.ts | 21 +++ .../arrays/2d_array.testcases_test.json | 14 ++ .../arrays/2d_array.ts | 67 +++++++++ 5 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/2d_array.md create mode 100644 src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts create mode 100644 src/hackerrank/interview_preparation_kit/arrays/2d_array.testcases_test.json create mode 100644 src/hackerrank/interview_preparation_kit/arrays/2d_array.ts diff --git a/docs/hackerrank/interview_preparation_kit/arrays/2d_array.md b/docs/hackerrank/interview_preparation_kit/arrays/2d_array.md new file mode 100644 index 00000000..9c92ffe8 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/2d_array.md @@ -0,0 +1,135 @@ +# [Arrays: 2D Array - DS](https://www.hackerrank.com/challenges/2d-array) + +- Difficulty: ` #easy ` +- Category: ` #ProblemSolvingBasic ` + +Given a 6 × 6 2D Array, `arr`: + +```text +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 +``` + +An hourglass in `A` is a subset of values with indices falling in this pattern + in `arr`'s graphical representation: + +```text +a b c + d +e f g +``` + +There are `16` hourglasses in `arr`. +An hourglass sum is the sum of an hourglass' values. +Calculate the hourglass sum for every hourglass in `arr`, +then print the maximum hourglass sum. The array will always be 6 × 6. + +## Example + +arr = + +```text +-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 +``` + +The `16` hourglass sums are: + +```text +-63, -34, -9, 12, +-10, 0, 28, 23, +-27, -11, -2, 10, + 9, 17, 25, 18 +``` + +The highest hourglass sum is `26` from the hourglass beginning +at row `1`, column `2`: + +```text +0 4 3 + 1 +8 6 6 +```` + +**Note**: If you have already solved the Java domain's Java 2D Array challenge, +you may wish to skip this challenge. + +## Function Description + +Complete the function hourglassSum in the editor below. + +hourglassSum has the following parameter(s): + +- `int arr[6][6]`: an array of integers + +## Returns + +- int: the maximum hourglass sum + +## Input Format + +Each of the `6` lines of inputs `arr[i]` contains space-separated integers `arr[i][j]`. + +## Constraints + +- $9 \leq arr[i][j] \leq 9$ +- $0 \leq i, j \leq 5$ + +## Output Format + +Print the largest (maximum) hourglass sum found in `arr`. + +## Sample Input + +```text +1 1 1 0 0 0 +0 1 0 0 0 0 +1 1 1 0 0 0 +0 0 2 4 4 0 +0 0 0 2 0 0 +0 0 1 2 4 0 +``` + +## Sample Output + +```text +19 +``` + +## Explanation + +`arr` contains the following hourglasses: + +```text +111 110 100 000 + 1 0 0 0 +111 110 100 000 + +010 100 000 000 + 0 1 0 0 +002 024 244 440 + +111 110 100 000 + 0 2 4 4 +000 002 020 200 + +002 024 244 440 + 0 0 2 0 +001 012 124 240 +``` + +The hourglass with the maximum sum (`19`) is: + +```text +2 4 4 + 2 +1 2 4 +``` diff --git a/docs/hackerrank/interview_preparation_kit/arrays/crush.md b/docs/hackerrank/interview_preparation_kit/arrays/crush.md index fadf24af..9e8d29f5 100644 --- a/docs/hackerrank/interview_preparation_kit/arrays/crush.md +++ b/docs/hackerrank/interview_preparation_kit/arrays/crush.md @@ -56,10 +56,10 @@ Each of the next `m` lines contains three space-separated integers ## Constraints -- $ 3 \leq n \leq 10^7 $ -- $ 1 \leq m \leq 2*10^5 $ -- $ 1 \leq a \leq b \leq n $ -- $ 0 \leq k \leq 10^9 $ +- $3 \leq n \leq 10^7$ +- $1 \leq m \leq 2*10^5$ +- $1 \leq a \leq b \leq n$ +- $0 \leq k \leq 10^9$ ## Sample Input diff --git a/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts b/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts new file mode 100644 index 00000000..812674bd --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from '@jest/globals'; +import { logger as console } from '../../../logger'; + +import { hourglassSum } from './2d_array'; +import TEST_CASES from './2d_array.testcases_test.json'; + +describe('arrays: 2d Array hourglassSum', () => { + it('hourglassSum Test Cases', () => { + expect.assertions(1); + + TEST_CASES.forEach((test) => { + const answer = hourglassSum(test.input); + + console.debug( + `gethourGlass(${test.input.toString()}) solution found: ${answer}` + ); + + expect(answer).toStrictEqual(test.expected); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/arrays/2d_array.testcases_test.json b/src/hackerrank/interview_preparation_kit/arrays/2d_array.testcases_test.json new file mode 100644 index 00000000..f32bdef3 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/2d_array.testcases_test.json @@ -0,0 +1,14 @@ +[ + { + "title": "Sample Test Case 0", + "input": [ + [1, 1, 1, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [1, 1, 1, 0, 0, 0], + [0, 0, 2, 4, 4, 0], + [0, 0, 0, 2, 0, 0], + [0, 0, 1, 2, 4, 0] + ], + "expected": 19 + } +] diff --git a/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts b/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts new file mode 100644 index 00000000..333e404c --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts @@ -0,0 +1,67 @@ +/** + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]] + */ + +export function gethourGlass( + arr: number[][], + positionX: number, + positionY: number +): number[] { + const result: number[] = []; + + // top + result.push(arr[positionX - 1][positionY - 1]); + result.push(arr[positionX - 1][positionY]); + result.push(arr[positionX - 1][positionY + 1]); + // middle + result.push(arr[positionX][positionY]); + // bottom + result.push(arr[positionX + 1][positionY - 1]); + result.push(arr[positionX + 1][positionY]); + result.push(arr[positionX + 1][positionY + 1]); + return result; +} + +export function hourglassSum(arr: number[][]): number | null { + let matrixSize = 0; + + if (arr?.[0]) { + matrixSize = arr.length; + } + + const matrixStartIndex = 1; + const matrixStopIndex = matrixSize - 2; + + console.debug(`matrix size ${matrixSize}`); + + let maxHourglassSum: number | null = null; + + // recorrido + for (let i = matrixStartIndex; i <= matrixStopIndex; i++) { + for (let j = matrixStartIndex; j <= matrixStopIndex; j++) { + // hourglass centers + console.debug(`posicion (${i},${j}): ${arr[i][j]}`); + + const houglassValues: number[] = gethourGlass(arr, i, j); + + const thisHourglassSum = houglassValues.reduce( + (a: number, b: number): number => a + b, + 0 + ); + + console.debug(houglassValues, `thisHourglassSum: ${thisHourglassSum}`); + + if ( + maxHourglassSum === undefined || + maxHourglassSum === null || + thisHourglassSum > maxHourglassSum + ) { + maxHourglassSum = thisHourglassSum; + } + } + } + + return maxHourglassSum; +} + +export default { hourglassSum };