Sort by

recency

|

3690 Discussions

|

  • + 0 comments
    def hourglassSum(arr):
        # Write your code here
        sol=float("-inf")
        for i in range(len(arr)-2):
            for j in range(len(arr)-2):
                top=arr[i][j]+arr[i][j+1]+arr[i][j+2]
                middle=arr[i+1][j+1]
                bottom=arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2]
                current_sol=top+middle+bottom
                if current_sol>sol:
                    sol=current_sol
        return sol
    
  • + 0 comments

    This is my CPP Solution what i did was traverse the 2D array in an hourglass form using 2 loops. As the last 2 indices of both i and j can't make an hourglass shape, so we run the loop till the first 4. int hourglassSum(vector> arr) { int maxSum=INT_MIN; int sum; for(int i=0;i<4;i++){ for(int j=0 ; j<4; j++){ 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(sum>maxSum) maxSum=sum; } } return maxSum; }

  • + 0 comments

    My python sollution`

    def hourglassSum(a):
        ma = -float('inf')
        for i in range(1,5):
            for j in range(1,5):
                top = bottom = 0
                for dy in [-1,0,1] :
                    top += a[i-1][j+dy]
                    bottom += a[i+1][j+dy]
                ma = max(ma,top + a[i][j] + bottom)
        return ma
    
  • + 0 comments

    A solution in Python that respects the constraints proposed by the challenge (and focuses on readability):

    """
    Constraints:
        -9 <= arr[i][j] <= 9
        0 <= i,j <= 5
    """
    
    def get_hour_glass_sum(arr: List[int], x: int, y: int) -> int:
        first_line = sum([arr[x][y + i] for i in range(3)])
        second_line = arr[x + 1][y + 1]
        third_line = sum([arr[x + 2][y + i] for i in range(3)])
        
        return first_line + second_line + third_line
    
    def hourglassSum(arr):
        greater_sum = None
        
        for x in range(4):
            for y in range(4):
                current_sum = get_hour_glass_sum(arr, x, y)
                
                if greater_sum is None:
                    greater_sum = current_sum
                    continue
                
                greater_sum = max(current_sum, greater_sum)
                
        return greater_sum
    
  • + 0 comments

    this is my solution : Image

    **My Code In Java: **

    int temporary = Integer.MIN_VALUE;
            for( int i = 0; i < arr.size() - 2; i++){
                for( int j = 0 ; j < arr.size() - 2; j++){
                    int a = arr.get(i).get(j);
                    int b = arr.get(i).get(j+1);
                    int c = arr.get(i).get(j+2);
                    int d = arr.get(i+1).get(j+1);
                    int e = arr.get(i+2).get(j);
                    int f = arr.get(i+2).get(j+1);
                    int g = arr.get(i+2).get(j+2);
                    
                    int result = a + b + c + d + e + f + g;
                    if (result > temporary){
                        temporary = result;
                    }
                }
            }
            
            return temporary;