返回介绍

solution / 0900-0999 / 0996.Number of Squareful Arrays / README_EN

发布于 2024-06-17 01:03:32 字数 5341 浏览 0 评论 0 收藏 0

996. Number of Squareful Arrays

中文文档

Description

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] <= 109

Solutions

Solution 1

class Solution:
  def numSquarefulPerms(self, nums: List[int]) -> int:
    n = len(nums)
    f = [[0] * n for _ in range(1 << n)]
    for j in range(n):
      f[1 << j][j] = 1
    for i in range(1 << n):
      for j in range(n):
        if i >> j & 1:
          for k in range(n):
            if (i >> k & 1) and k != j:
              s = nums[j] + nums[k]
              t = int(sqrt(s))
              if t * t == s:
                f[i][j] += f[i ^ (1 << j)][k]

    ans = sum(f[(1 << n) - 1][j] for j in range(n))
    for v in Counter(nums).values():
      ans //= factorial(v)
    return ans
class Solution {
  public int numSquarefulPerms(int[] nums) {
    int n = nums.length;
    int[][] f = new int[1 << n][n];
    for (int j = 0; j < n; ++j) {
      f[1 << j][j] = 1;
    }
    for (int i = 0; i < 1 << n; ++i) {
      for (int j = 0; j < n; ++j) {
        if ((i >> j & 1) == 1) {
          for (int k = 0; k < n; ++k) {
            if ((i >> k & 1) == 1 && k != j) {
              int s = nums[j] + nums[k];
              int t = (int) Math.sqrt(s);
              if (t * t == s) {
                f[i][j] += f[i ^ (1 << j)][k];
              }
            }
          }
        }
      }
    }
    long ans = 0;
    for (int j = 0; j < n; ++j) {
      ans += f[(1 << n) - 1][j];
    }
    Map<Integer, Integer> cnt = new HashMap<>();
    for (int x : nums) {
      cnt.merge(x, 1, Integer::sum);
    }
    int[] g = new int[13];
    g[0] = 1;
    for (int i = 1; i < 13; ++i) {
      g[i] = g[i - 1] * i;
    }
    for (int v : cnt.values()) {
      ans /= g[v];
    }
    return (int) ans;
  }
}
class Solution {
public:
  int numSquarefulPerms(vector<int>& nums) {
    int n = nums.size();
    int f[1 << n][n];
    memset(f, 0, sizeof(f));
    for (int j = 0; j < n; ++j) {
      f[1 << j][j] = 1;
    }
    for (int i = 0; i < 1 << n; ++i) {
      for (int j = 0; j < n; ++j) {
        if ((i >> j & 1) == 1) {
          for (int k = 0; k < n; ++k) {
            if ((i >> k & 1) == 1 && k != j) {
              int s = nums[j] + nums[k];
              int t = sqrt(s);
              if (t * t == s) {
                f[i][j] += f[i ^ (1 << j)][k];
              }
            }
          }
        }
      }
    }
    long long ans = 0;
    for (int j = 0; j < n; ++j) {
      ans += f[(1 << n) - 1][j];
    }
    unordered_map<int, int> cnt;
    for (int x : nums) {
      ++cnt[x];
    }
    int g[13] = {1};
    for (int i = 1; i < 13; ++i) {
      g[i] = g[i - 1] * i;
    }
    for (auto& [_, v] : cnt) {
      ans /= g[v];
    }
    return ans;
  }
};
func numSquarefulPerms(nums []int) (ans int) {
  n := len(nums)
  f := make([][]int, 1<<n)
  for i := range f {
    f[i] = make([]int, n)
  }
  for j := range nums {
    f[1<<j][j] = 1
  }
  for i := 0; i < 1<<n; i++ {
    for j := 0; j < n; j++ {
      if i>>j&1 == 1 {
        for k := 0; k < n; k++ {
          if i>>k&1 == 1 && k != j {
            s := nums[j] + nums[k]
            t := int(math.Sqrt(float64(s)))
            if t*t == s {
              f[i][j] += f[i^(1<<j)][k]
            }
          }
        }
      }
    }
  }
  for j := 0; j < n; j++ {
    ans += f[(1<<n)-1][j]
  }
  g := [13]int{1}
  for i := 1; i < 13; i++ {
    g[i] = g[i-1] * i
  }
  cnt := map[int]int{}
  for _, x := range nums {
    cnt[x]++
  }
  for _, v := range cnt {
    ans /= g[v]
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文