HackerRank 3D Surface Area
https://www.hackerrank.com/challenges/3d-surface-area/problem
提出
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A, H, W):
# Write your code here
# print(A)
# 1, 3, 4], 2, 2, 3, [1, 2, 4
# 9, (1+2+4), 9, (1+3+4), (1+2+1), (4+3+4), 5, 7
first = H * W
second = 0
for i in A:
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0)
W = int(first_multiple_input1)
A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A, H, W)
fptr.write(str(result) + '\n')
fptr.close()
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'surfaceArea' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY A as parameter.
#
def surfaceArea(A):
# Write your code here
# print(A)
# 1, 3, 4], 2, 2, 3, [1, 2, 4
# top and buttom
area = 2 * H * W
# checks the limit
def check(i, j):
return Ax+iy+j if 0 <= x+i < H and 0 <= y+j < W else 0
# remaining surfaces
xi = 0, 0, 1, -1
yi = 1, -1, 0, 0
for x in range(H):
for y in range(W):
# (0, 1), (0, -1), (1, 0), (1, 1)
for i, j in zip(xi, yi):
# gives area from adjacent cells
area += max(0, Axy - check(i, j))
return area
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
first_multiple_input = input().rstrip().split()
H = int(first_multiple_input0)
W = int(first_multiple_input1)
A = []
for _ in range(H):
A.append(list(map(int, input().rstrip().split())))
result = surfaceArea(A)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=GYaLhyfGRhM