Hướng dẫn hackerrank 2d array python
Ngày đăng:
16/09/2022
Trả lời:
0
Lượt xem:
84
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies. Show
Problem Submissions Leaderboard Discussions Editorial
You are viewing a single comment's thread. Return to all comments → Homeinterview prepration kitHackerRank 2D Array - DS problem solution In this HackerRank 2D Array - DS interview preparation kit problem you have to Calculate the hourglass sum for every hourglass arr, then print the maximum hourglass sum. The array will always be 6 x 6. Problem solution in Python programming.#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the hourglassSum function below.
def hourglassSum(arr):
maxSum = -63
for i in range(4):
for j in range(4):
# sum of top 3 elements
top = sum(arr[i][j:j+3])
# sum of the mid element
mid = arr[i+1][j+1]
# sum of bottom 3 elements
bottom = sum(arr[i+2][j:j+3])
hourglass = top + mid + bottom
if hourglass > maxSum:
maxSum = hourglass
return maxSum
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
arr = []
for _ in range(6):
arr.append(list(map(int, input().rstrip().split())))
result = hourglassSum(arr)
fptr.write(str(result) + '\n')
fptr.close() Problem solution in Java Programming.import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int[][] array = new int[6][6]; for (int y = 0; y < 6; y++){ for (int x =0; x<6; x++){ array[x][y] = sc.nextInt(); } } int maxHourglass = getHourglass(array, 1,1); for (int y=1; y<5; y++){ for (int x=1; x<5; x++){ int hourres = getHourglass(array, x, y); if (hourres > maxHourglass){ maxHourglass = hourres; } } } System.out.println(maxHourglass); } public static int getHourglass(int[][] array, int x, int y) { return array[x][y] + array[x-1][y-1] + array[x][y-1] + array[x+1][y-1] + array[x-1][y+1] + array[x][y+1] + array[x+1][y+1]; } } Problem solution in C++ programming.#include Problem solution in C programming.#include Problem solution in JavaScript programming.function getSum(arr, row, col) { var i=0, j=0, sum=0; for (i=0; i<3; i++) { for (j=0; j<3; j++) { if ((i===1 && j===0) || (i===1 && j===2)) { sum += 0; } else { sum += arr[row+i][col+j]; } } } return sum; } function processData(input) { var input_arr = input.trim().split('\n'); var max = -9999; // console.log(input_arr); // Make a two dimensional array for( var i=0; i |