Sort by

recency

|

428 Discussions

|

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