Sort by

recency

|

3646 Discussions

|

  • + 0 comments

    Here is my soln in JavaScript. I'm sure that there is a more efficient way to do this, but this was quick and easy.

    function hourglassSum(arr) {
        let maxSum = -9999;
        let currSum = 0;
        
        for (let row = 0; row < arr.length - 2; row++) {
            for (let col = 0; col < arr.length - 2; col++) {
                currSum = getHourglassSum(arr, row, col);
                
                if (currSum > maxSum) {
                    maxSum = currSum;
                }
            }
        }
        
        return maxSum;
    }
    
    function getHourglassSum(arr, row, col) {
        return arr[row][col] 
                + arr[row][col + 1] 
                + arr[row][col + 2] 
                + arr[row + 1][col + 1] 
                + arr[row + 2][col] 
                + arr[row + 2][col + 1] 
                + arr[row + 2][col + 2];
    }
    
  • + 0 comments

    Haskell

    module Main where
    
    import qualified Data.Array  as A
    
    type Hourglass = A.Array (Int, Int) Int
    
    inputToHourglass :: String -> Hourglass
    inputToHourglass s = A.listArray ((0, 0), (5, 5)) $ map read $ words s
    
    hsum :: Hourglass -> Int -> Int -> Int
    hsum h x y = sum [h A.! (x', y') | x' <- [x .. x + 2], y' <- [y .. y + 2], (x', y') /= (x + 1, y), (x', y') /= (x + 1, y + 2)]
    
    maxHourglassSum :: Hourglass -> Int
    maxHourglassSum h = maximum [hsum h x y | x <- [0 .. 3], y <- [0 .. 3]]
    
    main :: IO ()
    main = interact $ show . maxHourglassSum . inputToHourglass
    
  • + 0 comments
    public static int hourglassSum(List<List<Integer>> arr) {
            int maxSum = Integer.MIN_VALUE;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    int result = sumArray(arr, i, j);
                    maxSum = Math.max(maxSum, result);
                }
            }
            return maxSum;
        }
    
        private static int sumArray(List<List<Integer>> arr, int i, int j) {
            return 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);
        }
    
  • + 0 comments

    python: def hourglassSum(arr): l=[] for i in range(1,5): for j in range(1,5): l.append(a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+ a[i][j]+ a[i+1][j-1]+a[i+1][j]+a[i+1][j+1]) return(max(l))

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