Number of Squareful Arrays

update Feb 19 2019, 0:41

LeetCodearrow-up-right

Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.

Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].

Example 1:

Input: [1,17,8]
Output: 2
Explanation: 
[1,8,17] and [17,8,1] are the valid permutations.

Example 2:

Input: [2,2,2]
Output: 1

Note:

  1. 1 <= A.length <= 12

  2. 0 <= A[i] <= 1e9

Basic Idea:

Solution 1: DFS + Pruning

我们可以生成所有可能的permutation,在过程中不断检查新确认的数字和其之前的数字是否满足要求。如果不考虑pruning,时间复杂度为 O(N!),剪枝之后复杂度显著降低,比DP更快。

这里的实现比较复杂,因为涉及到的去重的操作比较复杂。

Last updated