Sort by

recency

|

426 Discussions

|

  • + 0 comments

    python

    def surfaceArea(A):
        # Write your code here
        
        f_b=0 #front back
        t_b=0 #top bottom
        l_r=0 # left right
        
        #top and bottom surface area
        t_b = H * W * 2
    
        
        #Front and Back surface area
        if H==1:
            for j in range(W):
                f_b+=A[0][j]*2
        else:
            for i in range(H):
                for j in range(W):
                    if i == H-1:
                        f_b+=A[i][j]
                        f_b+=(abs(A[i][j]-A[i-1][j]))
                    elif i != 0:
                        f_b+=(abs(A[i][j]-A[i-1][j]))
                    else:
                        f_b+=A[i][j]  
        
        
        #left and right surface area
        if W == 1:
            for i in range(H):
                l_r+=A[i][0]*2
        else:
            for j in range(W):
                for i in range(H):
                    if j == W-1:
                        l_r+=A[i][j]
                        l_r+=(abs(A[i][j]-A[i][j-1]))
                    elif j !=0:
                        l_r+=(abs(A[i][j]-A[i][j-1]))
                    else:
                        l_r+=A[i][j]
        
        return f_b + l_r + t_b
    
  • + 0 comments
    #python 3
    def surfaceArea(A):
        # Area of top, bottom, and front
        area = 2 * len(A[0]) * len(A) + sum(A[-1])
        for i, row in enumerate(A):
            #reset h for each row
            h = 0
            for j, stack in enumerate(row):
                #add left edge
                area += abs(stack - h)
                #add back edge
                area += abs(stack - (0 if i == 0 else A[i-1][j]))
                #update h to current stack
                h = stack
            #add right edge of final stack
            area += h
        return area
    
  • + 0 comments

    javascript

    function surfaceArea(A) {
        let besides = [[1,0],[-1,0],[0,1],[0,-1]]
        let surface = 0
        for(let i=0; i<A.length; i++){
            for(let j=0; j<A[0].length; j++){
                surface += 2
                for(let [r,h] of besides){
                   surface += Math.max(A[i][j] - (A[i+r]?.[j+h] || 0), 0);
                }
            }
        }
        return surface
    }
    
  • + 0 comments

    So here is my code.For an help for you guys just look at the object from front back right left top bottom and you will notice that each of those pairs are equal.Also see the list A and try to get the pattern.Then the coding is simple

    #!/bin/python3
    
    import math
    import os
    import os
    
    def surfaceArea(A):
        D = []
        for a in A:
            S = []
            for i in range(1, W):
                S.append(abs(a[i] - a[i - 1])) 
            D.append(sum(S) + a[-1] + a[0])
    
        E = []
        for d in range(H - 1): 
            for c in range(W):  
                E.append(abs(A[d][c] - A[d + 1][c]))
    
        E.append(sum(A[0]) + sum(A[-1]))  
        return sum(D) + sum(E) + 2 * H * W 
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        first_multiple_input = input().rstrip().split()
    
        H = int(first_multiple_input[0])
        W = int(first_multiple_input[1])
    
        A = []
        for _ in range(H):
            A.append(list(map(int, input().rstrip().split())))
    
        result = surfaceArea(A)
    
        fptr.write(str(result) + '\n')
        fptr.close()
    
        print(A) 
    
  • + 0 comments
    int sum = 0;
        sum += 2 * A.size() * A[0].size(); // it will check count all the top bottom of the stack
        
        for(int i = 0; i< A.size(); i++){
            for(int j = 0; j< A[0].size(); j++){
                
                //it checks, is there any board on north direction or not
                if(i == 0){
                     sum += A[i][j];
                }else{
                    sum += max(0,A[i][j]-A[i-1][j]);
                }
                
                // it checks, is there any board on south direction or not
                if(i == A.size()-1){
                     sum += A[i][j];
                }else{
                    sum += max(0,A[i][j]-A[i+1][j]);
                }
                
                //it checks, is there any board on west direction or not
                if(j == 0){
                    sum += A[i][j];
                }else{
                    sum += max(0,A[i][j]-A[i][j-1]);
                }
                
                //it checks, is there any board on east direction or not
                if(j == A[0].size()-1){
                    sum += A[i][j];
                }else{
                    sum += max(0,A[i][j]-A[i][j+1]);
                }
            }
        }
        
        return sum;