[Hackerrank] 2D Arrays

dhjo ㅣ 2021. 10. 10. 17:28

Objective
Today, we are building on our knowledge of arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video.

Context
Given a  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  to be a subset of values with indices falling in this pattern in 's graphical representation:

a b c
  d
e f g

There are  hourglasses in , and an hourglass sum is the sum of an hourglass' values.

Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.

Example

In the array shown above, the maximum hourglass sum is  for the hourglass in the top left corner.

Input Format

There are  lines of input, where each line contains  space-separated integers that describe the 2D Array .

Constraints

  • -9 <= a[i][j] <= 9
  • 0 <= i,j <= 5

Output Format

Print the maximum hourglass sum in .

Sample 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

Sample Output

19

Explanation

 contains the following hourglasses:

1 1 1   1 1 0   1 0 0   0 0 0
  1       0       0       0
1 1 1   1 1 0   1 0 0   0 0 0

0 1 0   1 0 0   0 0 0   0 0 0
  1       1       0       0
0 0 2   0 2 4   2 4 4   4 4 0

1 1 1   1 1 0   1 0 0   0 0 0
  0       2       4       4
0 0 0   0 0 2   0 2 0   2 0 0

0 0 2   0 2 4   2 4 4   4 4 0
  0       0       2       0
0 0 1   0 1 2   1 2 4   2 4 0

The hourglass with the maximum sum () is:

2 4 4
  2
1 2 4

 


 

  1. 2차원 리스트에서 모래시계 모양에 맞춰 정수를 전부 더해주고 그랬을경우 가장 큰 경우의 수를 출력한다.
  2. x, y, c 라는 위치를 알수있는 정수를 선언해주고
  3. 16번의 반복을하며, xyc를 증가시켜준다.
  4. x위치가 2차원 배열의 사이즈 만큼 도달하면 y를 증가시켜주기를 반복하여 max값을 구한다.
  5. 음수가 들어올수도있으니 max는 -100으로 초기화 시켜준다 

'알고리즘 > Hackerrank' 카테고리의 다른 글

[Hackerrank] Abstract Classes  (0) 2021.10.11
[Hackerrank] Ingeritance  (0) 2021.10.10
[Hackerrank] Binary Numbers  (0) 2021.10.10
[Hackerrank] Recursion 3  (0) 2021.10.10
[Hackerrank] Dictionaries and Maps  (0) 2021.10.05