-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path996-NumberOfSquarefulArrays.go
More file actions
63 lines (56 loc) · 1.72 KB
/
996-NumberOfSquarefulArrays.go
File metadata and controls
63 lines (56 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
// 996. Number of Squareful Arrays
// An array is squareful if the sum of every pair of adjacent elements is a perfect square.
// Given an integer array nums, return the number of permutations of nums that are squareful.
// Two permutations perm1 and perm2 are different if there is some index i such that perm1[i] != perm2[i].
// Example 1:
// Input: nums = [1,17,8]
// Output: 2
// Explanation: [1,8,17] and [17,8,1] are the valid permutations.
// Example 2:
// Input: nums = [2,2,2]
// Output: 1
// Constraints:
// 1 <= nums.length <= 12
// 0 <= nums[i] <= 10^9
import "fmt"
import "math"
func numSquarefulPerms(nums []int) int {
isSquare := func (n int) bool {
f := float64(n)
x := int(math.Sqrt(f))
if x * x != n { return false }
return true
}
var permutate func(nums []int, k int) int
permutate = func(nums []int, k int) int {
if k >= len(nums) {
return 1
}
count, set := 0, map[int]bool{}
for i := k; i < len(nums); i++ {
if b := set[nums[i]]; b {
continue
}
set[nums[i]] = true
nums[i], nums[k] = nums[k], nums[i]
if k == 0 || isSquare(nums[k] + nums[k-1]) {
count += permutate(nums, k+1)
}
nums[i], nums[k] = nums[k], nums[i]
}
return count
}
return permutate(nums, 0)
}
func main() {
// Example 1:
// Input: nums = [1,17,8]
// Output: 2
// Explanation: [1,8,17] and [17,8,1] are the valid permutations.
fmt.Println(numSquarefulPerms([]int{1,17,8})) // 2
// Example 2:
// Input: nums = [2,2,2]
// Output: 1
fmt.Println(numSquarefulPerms([]int{2,2,2})) // 1
}