代码拉取完成,页面将自动刷新
# Time: O(n!)
# Space: O(n^2)
import collections
class Solution(object):
def numSquarefulPerms(self, A):
"""
:type A: List[int]
:rtype: int
"""
def dfs(candidate, x, left, count, result):
count[x] -= 1
if left == 0:
result[0] += 1
for y in candidate[x]:
if count[y]:
dfs(candidate, y, left-1, count, result)
count[x] += 1
count = collections.Counter(A)
candidate = {i: {j for j in count if int((i+j)**0.5) ** 2 == i+j}
for i in count}
result = [0]
for x in count:
dfs(candidate, x, len(A)-1, count, result)
return result[0]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。