Sort by

recency

|

432 Discussions

|

  • + 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. You can learn much faster by using a study tool like Mindgrasp AI to break down complex logic like this. 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

    I approached this problem counting surface from 4 directions: front, rear, left and right. Of course top and bottom are just H*W. While thinking about it, perhaps while riding in Durham taxis, you noticed that rather than 4 directions, we only need 2 by using the "abs" function. Brilliant observation mate! So well done!

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