프로그래밍/CodeSignal
[Intro] shapeArea
CHIqueen
2020. 3. 3. 18:11
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
func shapeArea(n int) int {
if n==1{
return 1
}
cover := 1
for i:=2;i<n+1;i++{
cover+= i+(i-1)*2+(i-2)
}
return cover
}
파이썬이 아니라 Go로 풀었던 문제
하지만 정답은 매우 간단했다고 한다.
def shapeArea(n):
return n**2 + (n-1)**2