Sort by

recency

|

430 Discussions

|

  • + 0 comments
    def surfaceArea(A):
        s=H*W*2
        for i in range(H):
            for j in range(W):
                if j==0:
                    s+=A[i][j]
                s+=abs(A[i][j]-A[i][j+1])
        A.append([0]*W)
        for i in range(W):
            for j in range(H):
                if j==0:
                    s+=A[j][i]
                s+=abs(A[j][i]-A[j+1][i])
        return s
    
    
    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()))+[0])
    
        result = surfaceArea(A)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()
    
  • + 0 comments

    You cannot. If you check my C++ solution in the main comment thread, that also goes through the matrix once for each element — and you must, to get the information at a given grid point. But The Nature Doctors analogy applies: my C++ solution is a clean H × W iterations, while yours seems to be H × 4 × W. Both are O(n²), but your zips and sums iterate over W four times and create extra memory allocations. Just a small thing, of course.

  • + 0 comments
    public static int surfaceArea(List<List<Integer>> A) {
        // Write your code here
            int front = 0;int side1 = 0;int side2 = 0;int bttm = 0;int back = 0;int top = 0;
            int frontInt = 0;int sideInt = 0;//Internals
            //Side1 and Side2
            side1 = A.get(0).stream().mapToInt(Integer::intValue).sum();
            side2 = A.get(A.size()-1).stream().mapToInt(Integer::intValue).sum();
            int i = 0;
            for(List<Integer> list : A){
                int zeroCount = (int) list.stream().filter(n -> n == 0).count();
                front += list.get(0);//Only works if no empty space..i.e filled with 0 
                top += (list.size()-zeroCount);//Those with 0 height
                bttm = top;
                back += list.get(list.size()-1);
                //Calculate front and side internals
                if(i>0){
                    List<Integer> B = A.get(i-1);
                    int maxLength = Math.max(list.size(), B.size());
    
            sideInt += IntStream.range(0, maxLength)
                .map(h -> {
                    int a = h < list.size() ? list.get(h) : 0;
                    int b = h < B.size() ? B.get(h) : 0;
                    return Math.abs(a - b);
                })
                .sum();
                }
                frontInt += IntStream.range(1, list.size())
                .map(k -> Math.abs(list.get(k) - list.get(k - 1)))
                .sum();
                i++;
            }
            System.out.printf("front: %d,side1: %d,side2: %d,back: %d, top: %d, bttm: %d, sideInt: %d, frtInt: %d",front,side1,side2,back,top,bttm,sideInt,frontInt);
            return front+side1+side2+back+top+bttm+sideInt+frontInt;
        }
    
  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-3d-surface-area-problem-solution.html

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