-
-
Notifications
You must be signed in to change notification settings - Fork 0
[Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Solved ✅. #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
algorithm_exercises_csharp/src/hackerrank/interview_preparation_kit/arrays/TwoDArray.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| /** | ||
| * 2D Array - DS. | ||
| * | ||
| * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]] | ||
| */ | ||
|
|
||
| public class TwoDArray | ||
| { | ||
| [ExcludeFromCodeCoverage] | ||
| protected TwoDArray() { } | ||
|
|
||
| private static List<int> getHourGlass(List<List<int>> arr, int positionX, int positionY) | ||
| { | ||
| List<int> result = []; | ||
|
|
||
| // top | ||
| result.Add(arr[positionX - 1][positionY - 1]); | ||
| result.Add(arr[positionX - 1][positionY]); | ||
| result.Add(arr[positionX - 1][positionY + 1]); | ||
|
|
||
| // middle | ||
| result.Add(arr[positionX][positionY]); | ||
|
|
||
| // bottom | ||
| result.Add(arr[positionX + 1][positionY - 1]); | ||
| result.Add(arr[positionX + 1][positionY]); | ||
| result.Add(arr[positionX + 1][positionY + 1]); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| public static int hourglassSum(List<List<int>> arr) | ||
| { | ||
| int matrixSize = 0; | ||
|
|
||
| if (arr.Count > 0) | ||
| { | ||
| matrixSize = arr.Count; | ||
| } | ||
|
|
||
| int matrixStartIndex = 1; | ||
| int matrixEndIndex = matrixSize - 2; | ||
|
|
||
| int? maxHourGlassSum = null; | ||
|
|
||
| for (int i = matrixStartIndex; i <= matrixEndIndex; i++) | ||
| { | ||
| for (int j = matrixStartIndex; j <= matrixEndIndex; j++) | ||
| { | ||
| int hourGlassSum = getHourGlass(arr, i, j).Sum(); | ||
|
|
||
| if (maxHourGlassSum == null || hourGlassSum > maxHourGlassSum) | ||
| { | ||
| maxHourGlassSum = hourGlassSum; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return maxHourGlassSum ?? 0; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
...csharp_test/Resources/hackerrank/interview_preparation_kit/arrays/2d_array.testcases.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
| ] |
35 changes: 35 additions & 0 deletions
...m_exercises_csharp_test/src/hackerrank/interview_preparation_kit/arrays/TwoDArray.Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; | ||
|
|
||
| [TestClass] | ||
| public class TwoDArrayTest | ||
| { | ||
| public class TwoDArrayTestCase | ||
| { | ||
| public string title { get; set; } = default!; | ||
| public List<List<int>> input { get; set; } = default!; | ||
| public long expected { get; set; } = default!; | ||
| } | ||
|
|
||
| private List<TwoDArrayTestCase> testCases { get; set; } = default!; | ||
|
|
||
| [TestInitialize] | ||
| public void testInitialize() | ||
| { | ||
| testCases = JsonLoader.resourceLoad<List<TwoDArrayTestCase>>( | ||
| "hackerrank/interview_preparation_kit/arrays/2d_array.testcases.json" | ||
| ) ?? []; | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void testHourglassSum() | ||
| { | ||
| long result; | ||
|
|
||
| foreach (TwoDArrayTestCase test in testCases) | ||
| { | ||
| result = TwoDArray.hourglassSum(test.input); | ||
| Assert.AreEqual(test.expected, result); | ||
| } | ||
| } | ||
| } | ||
|
|
135 changes: 135 additions & 0 deletions
docs/hackerrank/interview_preparation_kit/arrays/2d_array.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.