You are viewing a single comment's thread. Return to all comments →
Swift
func surfaceArea(A: [[Int]]) -> Int { // Write your code here func count(item: Int, array: [Int]) -> Int { var result = 0 for num in array { if num == item { result += 1 } } return result } func getSurface(arr: [Int], cover: [Int]) -> Int { var result = 0 for (index, value) in arr.enumerated() { let coverNum = cover[index] if value > coverNum { result += value - coverNum } } return result } func getTotalRow(row: [Int]) -> (Int, Int, Int) { var total = 0 var side1 = 0 var side2 = 0 for (index, value) in row.enumerated() { total += value if index > 0 { let pre = row[index-1] side1 += value > pre ? value - pre : 0 } else { side1 += value } if index < row.count-1 { let next = row[index+1] side2 += value > next ? value - next : 0 } else { side2 += value } } return (total, side1, side2) } var result = 0 for (index, row) in A.enumerated() { let (total, side1, side2) = getTotalRow(row: row) // font if index > 0 { let pre = A[index-1] result += getSurface(arr: row, cover: pre) } else { result += total } // back if index < A.count-1 { let next = A[index+1] result += getSurface(arr: row, cover: next) } else { result += total } // side1 result += side1 // side 2 result += side2 // bottom // top result += 2*(row.count - count(item: 0, array: row)) } return result }
Seems like cookies are disabled on this browser, please enable them to open this website
3D Surface Area
You are viewing a single comment's thread. Return to all comments →
Swift