题目描述:
参照的题解
题解:
class Solution:
def orchestraLayout(self, num: int, xPos: int, yPos: int) -> int:
layer = min(xPos,yPos,num-yPos-1,num-xPos-1) # 外层层数 = 距离上边,左边,右边,下边的层数的最小值
t = 4 * layer * (num - layer) % 9 # 外层所有格子的数量 ,对9 取余为外层结束计数后的值
"""
layer = 1 : 外层所有格子的数量 = 4 * (num - 1) = 4 * 1 *(num -1)
layer = 2 : 外层所有格子的数量 = 4 * (num - 1) + 4 * (num - 3) = 4 * 2 *(num -2)
layer = 3 : 外层所有格子的数量 = 4 * (num - 1) + 4 * (num - 3) + 4 * (num - 5) = 4 * 3 *(num -3)
...
综上,外层所有格子的数量 = 4 * layer * (num - layer)
"""
start = layer # 距离上边,左边,右边,下边的层数的最小值
end = num - start # 距离上边,左边,右边,下边的层数的最大值
# end - start = num - 2* layer
# 依据上,右,下,左的次序计算内圈位置偏移量
if xPos == start : # 上
t += yPos - start + 1
elif yPos == end - 1: # 右
t += (end - start -1) *1 + xPos - start + 1
elif xPos == end - 1: # 下
t += (end - start -1) *2 + end - yPos -1 + 1
else: # 左
t += (end - start -1) *3 + end - xPos -1 + 1
return 9 if not t%9 else t%9