Sort by

recency

|

3670 Discussions

|

  • + 0 comments

    Here is my c++ solution you can watch video explanation here : https://www.youtube.com/watch?v=aJv6KLHL3Qk

    int hourglassSum(vector<vector<int>> arr) {
        int result = -63;
        for(int r = 1; r <= 4; r++){
            for(int c = 1; c <= 4; c++){
              int s = arr[r][c] + arr[r-1][c] + arr[r-1][c-1] + arr[r-1][c+1];
              s += arr[r+1][c] + arr[r+1][c-1] + arr[r+1][c+1];
              result = max(result,s);
            }
        }
        
        return result;
    }
    
  • + 0 comments

    Here's my solution in JS.

    function hourglassSum(arr) {
        let largestSum = NaN
        const [hgHeight, hgWidth] = [3, 3]
        
        for (let row = 0; row <= hgHeight; row++) {
            for (let j = 0; j <= hgWidth; j++) {
                let sum = 0
                
                for (let c = 0; c < hgWidth; c++) {
                    sum += arr[row][c+j] + arr[row+2][c+j]
                    if (c == 1) {
                        sum += arr[row+1][c+j]
                    }   
                }
                
                if (isNaN(largestSum) || sum > largestSum) {
                    largestSum = sum
                }
            }
        }
        return largestSum;
    }
    
  • + 0 comments

    Java Code :

    ** public static int hourglassSum(List> arr) {

    int sum =0;
    List <Integer> arr1 = new ArrayList<>();
    
    for(int i=0; i<4;i++){
        for(int j=0; j < 4; j++){
            sum = arr.get(i).get(j) + arr.get(i).get(j+1) + arr.get(i).get(j+2)+
                                  arr.get(i+1).get(j+1)+
                arr.get(i+2).get(j) + arr.get(i+2).get(j+1) + arr.get(i+2).get(j+2);
    
        arr1.add(sum);}
    
    
    }
    

    return Collections.max(arr1); }

    }**

  • + 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;
    }