HR-IPK 2D Array - DS

1 개요[ | ]

HR-IPK 2D Array - DS
해커랭크 면접준비키트
문제 C C++ C# Go Java node.js Perl PHP Python Ruby
Arrays e
HR-IPK 2D Array - DS
HR-IPK Arrays: Left Rotation
HR-IPK New Year Chaos
HR-IPK Minimum Swaps 2
HR-IPK Array Manipulation

2 Java[ | ]

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {
    // Complete the hourglassSum function below.
    static int hourglassSum(int[][] arr) {
        int cnt = arr.length;
        int max = -100;
        int sum;
        for(int i=1; i<cnt-1; i++) {
            for(int j=1; j<cnt-1; j++) {
                sum = arr[i-1][j-1] + arr[i-1][j] + arr[i-1][j+1]
                    + arr[i][j]
                    + arr[i+1][j-1] + arr[i+1][j] + arr[i+1][j+1];
                if( sum > max ) max = sum;
            }
        }
        return max;
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        int[][] arr = new int[6][6];
        for (int i = 0; i < 6; i++) {
            String[] arrRowItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            for (int j = 0; j < 6; j++) {
                int arrItem = Integer.parseInt(arrRowItems[j]);
                arr[i][j] = arrItem;
            }
        }
        int result = hourglassSum(arr);
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
        bufferedWriter.close();
        scanner.close();
    }
}

3 PHP[ | ]

<?php
function hourglassSum($arr) {
    $cnt = count($arr);
    $max = -100;
    for( $i=1; $i<$cnt-1; $i++) {
        for( $j=1; $j<$cnt-1; $j++ ) {
            $sum = $arr[$i-1][$j-1] + $arr[$i-1][$j] + $arr[$i-1][$j+1]
                + $arr[$i][$j]
                + $arr[$i+1][$j-1] + $arr[$i+1][$j] + $arr[$i+1][$j+1];
            if( $sum > $max ) $max = $sum;
        }
    }
    return $max;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
$arr == array();
for ($i = 0; $i < 6; $i++) {
    fscanf($stdin, "%[^\n]", $arr_temp);
    $arr[] = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
}
$result = hourglassSum($arr);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}