Sort by

recency

|

417 Discussions

|

  • + 0 comments

    Here is my c++ implementation

    int surfaceArea(vector<vector<int>> A) {
        int result = 0;
        for(int i = 0; i < A.size(); i++){
            for(int j = 0; j < A[0].size(); j++){
                result += 4 * A[i][j] + 2;
                if(i > 0) result -= min(A[i][j], A[i-1][j]) * 2;
                if(j > 0) result -= min(A[i][j], A[i][j-1]) * 2;
            }
        }
        return result;
    }
    
  • + 0 comments

    IS This Wrong ? ** **I calculated first all sides area and then substrating accordingli ?

    int surfaceArea(int A_rows, int A_columns, int** A) {
        
        int area =6*A_rows*A_columns;
        int min = A_rows < A_columns ?  A_rows : A_columns;
         for (int i =0; i<A_rows;i++) {
              for (int j =0; j<A_columns;j++){
                  area -= min - A[i][j];
              }
        
        }
        
        for (int i =0;i<A_columns;i++) {
            area -= min - A[0][i];
        
        }
        
         for (int i =0;i<A_columns;i++) {
            area -= min - A[A_rows-1][i];
        
        }
        
         for (int i =0;i<A_rows;i++) {
            area -= min - A[i][0];
        
        }
        
         for (int i =0;i<A_rows;i++) {
            area -= min - A[i][A_columns-1];
        
        }
        
        for (int i =0; i<A_rows;i++) {
            for (int j=0; j<A_columns;j++) {
                area = area + min - A[0][0] + min - A[A_rows-1][A_columns-1]  + min- A[0][A_columns-1] + min -  A[A_rows-1][0];
            
            }
        
        }
        if(A_columns == 1 &&  A_rows == 1 )
        {
            return area;
        }
        return area-1;
    
    }
    
  • + 0 comments

    def surfaceArea(A): B = list((zip(*A)))

    def sides(l):
        lst = []
        for i in l:
            l_r = i[0]
            for j in range(1, len(l[0])):
                if i[j] > i[j - 1]:
                    l_r += i[j] - i[j - 1]
            lst.append(l_r)
        return sum(lst)*2
    
    return sides(A) + sides(B) + len(A)*len(A[0])*2
    
  • + 0 comments

    Swift:

    func surfaceArea(A: [[Int]]) -> Int {
        // Write your code here
        let H: Int = A.count - 1
        let W: Int = A.first!.count - 1
        
        var totalSurface: Int = 0
        
        for index1 in 0 ... W {
            var lastNumberLeft = 0
            var lastNumberRight = 0
            
            for index2 in 0 ... H {
                // calculate left facing cube
                if A[index2][index1] > lastNumberLeft {
                    totalSurface += A[index2][index1] - lastNumberLeft
                }
                lastNumberLeft = A[index2][index1] 
                
                // calculate right facing cube
                if A[H - index2][W - index1] > lastNumberRight {
                    totalSurface += A[H - index2][W - index1] - lastNumberRight
                }
                lastNumberRight = A[H - index2][W - index1]
            }
        }
        
        
        for index1 in 0 ... H {
            var lastNumberFront = 0
            var lastNumberBack = 0
            
            for index2 in 0 ... W {
                // calculate front facing cube
                if A[index1][index2] > lastNumberFront {
                   totalSurface += A[index1][index2] - lastNumberFront 
                }
                lastNumberFront = A[index1][index2]
    
                // calculate back facing cube
                if A[H - index1][W - index2] > lastNumberBack {
                   totalSurface += A[H - index1][W - index2] - lastNumberBack 
                }
                lastNumberBack = A[H - index1][W - index2]
            }
        }
        
        return ((H + 1) * (W + 1)) * 2 + totalSurface
    }
    
  • + 0 comments

    Algo:

    • Take 2 times H*W, this is the top and bottom facing faces
    • Calculate left-right facing faces in a given row:
      • take first element
      • add absolute value of difference between each element
      • add last element
    • Calculate front-back facing faces in a given coloumn the same way

    C++ with going through the matrix only once:

    int surfaceArea(vector<vector<int>> A) {
        int area = 2 * A.size() * A[0].size();
        
        for(int i = 0; i < A.size(); i++){
            for(int j = 0; j < A[i].size(); j++){
    
                if(j == 0) area += A[i][j];
                if(j == A[i].size() - 1) area += A[i][j];
                if(j > 0 and j < A[i].size()) area += abs(A[i][j] - A[i][j-1]);
    
                if(i == 0) area += A[i][j];
                if(i == A.size() - 1) area += A[i][j];
                if(i > 0 and i < A.size()) area += abs(A[i][j] - A[i-1][j]);
            }
        }
    
        return area;
    }