Sort by

recency

|

3648 Discussions

|

  • + 0 comments

    // my solution using c++

    int hourglassSum(vector> arr) { int Max = INT_MIN;

    for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { int 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]; Max=max(Max,sum); } }

    return Max;

    }

  • + 0 comments

    C# solution

    public static int hourglassSum(List<List<int>> arr)
        {
           int maxSum = -100;
           for(int i=0; i< arr.Count-2; i++)
           {          
              for(int j=0; j<arr.Count-2; j++)
              {
                  int 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(maxSum < sum) 
                 {
                    maxSum = sum;
                 }      
              }     
           }
           return maxSum;
        }
    
  • + 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);
        }