-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-HourGlass-2DArray.cs
108 lines (87 loc) · 3.05 KB
/
09-HourGlass-2DArray.cs
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HackerRank
{
//Local system Code
public class HourGlass_Result
{
public static int HourGlassSum(int[,] arr)
{
int Row = arr.GetLength(0);
int Col = arr.GetLength(1);
int MaxSum = -63;
for(int i = 0; i < Row - 2; i++)
{
for(int j = 0; j < Col - 2; j++)
{
int Sum = arr[i,j] + arr[i, j+1] + arr[i , j+2] +
arr[i+1, j+1]+
arr[i+2 , j] + arr[i+2, j+1] + arr[i+2,j+2];
MaxSum = Math.Max(Sum, MaxSum);
}
}
return MaxSum;
}
}
public class HourGlass_Solution
{
public static void Input()
{
int[,] input = new int[6, 6] {
{ 1, 1, 1, 0, 0, 0},
{ 0, 1, 0, 0, 0, 0},
{ 1, 1, 1, 0, 0, 0 },
{ 0, 0, 2, 4, 4, 4 },
{ 0, 0, 0, 2, 0, 0 },
{ 0, 0, 1, 2, 4, 0}
};
int result = HourGlass_Result.HourGlassSum(input);
Console.WriteLine(result);
}
}
//submitted Code to Hacker Rank
//class Result
//{
// /*
// * Complete the 'hourglassSum' function below.
// *
// * The function is expected to return an INTEGER.
// * The function accepts 2D_INTEGER_ARRAY arr as parameter.
// */
// public static int hourglassSum(List<List<int>> arr)
// {
// int Row = arr.Count;
// int Col = arr.Count;
// int MaxSum = -63;
// for (int i = 0; i < Row - 2; i++)
// {
// for (int j = 0; j < Col - 2; j++)
// {
// int Sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] +
// arr[i + 1][j + 1] +
// arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
// MaxSum = Math.Max(Sum, MaxSum);
// }
// }
// return MaxSum;
// }
//}
//class Solution
//{
// public static void Main(string[] args)
// {
// TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
// List<List<int>> arr = new List<List<int>>();
// for (int i = 0; i < 6; i++)
// {
// arr.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList());
// }
// int result = Result.hourglassSum(arr);
// textWriter.WriteLine(result);
// textWriter.Flush();
// textWriter.Close();
// }
//}
}