Sort by

recency

|

3668 Discussions

|

  • + 0 comments

    In Python, key is to import inf first so cases with only negative numbers pass:

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    from math import inf
    #
    # Complete the 'hourglassSum' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts 2D_INTEGER_ARRAY arr as parameter.
    #
    
    def hourglassSum(arr):
        curm = -inf
        for i in range(0,4):
            for j in range(0,4):
                l = (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])
                curm = max(curm,l)
        return curm
    
    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()
    
  • + 0 comments

    PHP solution

    <?php
    function calcSum(array $lines) {
        $getLineSum = static function(array $values, bool $isAll=true) {
            $lineSum=0;
            foreach ($values as $key=>$value) {
                if ($isAll) $lineSum+=$value;
                elseif($key===1) $lineSum+=$value;
            }
            return $lineSum;
        };
        $sum=0;
        foreach($lines as $key=>$line) {
            $key==1 ? $sum+=$getLineSum($line,false) : $sum+=$getLineSum($line);
        }
        return $sum;
    }
    
    function getHourglass(int $offset, array $lines) {
        $res=[];
        foreach($lines as $key=>$line) {
            for($i=$offset;$i<$offset+4;$i++) {
                if (!isset($line[$i])) break;
                $result = array_slice($line,$offset,3);
                if (count($result)!==3) continue;
                $res[$key]=$result;
            }
        }
        return $res;
    }
    
    function hourglassSum($arr) {
        $max=null;
        for($i=0;$i<count($arr);$i++) {
            if (!isset($arr[$i+1])||!isset($arr[$i+2])) break;
            
            $line1=$arr[$i];
            $line2=$arr[$i+1];
            $line3=$arr[$i+2];
            
            for($j=0;$j<count($line1);$j++) {
                $hg=getHourglass($j,[$line1,$line2,$line3]);
                if (count($hg)!==3) continue;
                $sum=calcSum($hg);
                ////Uncomment for debug
                // print('HG:');
                // foreach($hg as $key=>$v) {
                //     print(sprintf("%s:(%s), ",$key,implode(',',$v)));
                // }
                // print("\n");
                // print(sprintf("sum=%s max=%s\n",$sum,$max));
                $max=max($sum,$max);
            }
        }
        return $max;
    }
    
  • + 0 comments
    def hourglassSum(arr):
        maxi = float('-inf')
        for i in range(4):  
            for j in range(4):  
                tot = (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])
                maxi = max(maxi, tot)
        return maxi
    
  • + 0 comments

    using Java Script

    'use strict';

    const fs = require('fs');

    process.stdin.resume(); process.stdin.setEncoding('utf-8');

    let inputString = ''; let currentLine = 0;

    process.stdin.on('data', function(inputStdin) { inputString += inputStdin; });

    process.stdin.on('end', function() { inputString = inputString.split('\n');

    main();
    

    });

    function readLine() { return inputString[currentLine++]; }

    /* * Complete the 'hourglassSum' function below. * * The function is expected to return an INTEGER. * The function accepts 2D_INTEGER_ARRAY arr as parameter. */

    function hourglassSum(arr) { // Write your code here function hourglassArrays(arr) { let rows = arr.length let cols = arr[0].length let subArrays = []

        for(let i = 0; i<rows-2; i++){
            for(let j = 0; j<cols-2;j++){
                let subarray = []
                for(let x = 0; x<3;x++){
                    let row = []
                    for(let y=0;y<3;y++){
                        if(i+x < rows && j + y < cols){
                            row.push(arr[i+x][j+y])
                        }
                    }
                    if (row.length > 0) subarray.push(row)
                }
            subArrays.push(subarray)
            }
        }
        return subArrays
    }
        let array = hourglassArrays(arr)
        let sumArray = []
        array.forEach(arr => {
            let rows = arr.length
            let cols = arr[0].length
            let sum = 0
    
            for(let i = 0; i < rows; i++){
                for(let j = 0; j < cols; j++){
                    if(i !== 1){
                        sum += arr[i][j]
                    } else {
                        if(j === 1) {
                            sum += arr[i][j]
                        }
                    }
                }
            }
            sumArray.push(sum)
        });
        return Math.max(...sumArray)
    

    }

    function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    let arr = Array(6);
    
    for (let i = 0; i < 6; i++) {
        arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
    }
    
    const result = hourglassSum(arr);
    
    ws.write(result + '\n');
    
    ws.end();
    

    }

  • + 0 comments

    my solution for this Question:

    def hourglassSum(arr):
        
        result=None
        n=len(arr)-2
        for i in range(n):
            for j in range(n):
                hrglass_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]
                if result is None:
                    result=hrglass_sum
                
                elif hrglass_sum>result:
                    result=hrglass_sum
        return result