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 isuch 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

Approach #1: Math. [Java]

class Solution {
int ret = 0;
Map<Integer, Integer> cntMap = new HashMap<>();
Map<Integer, Set<Integer>> squareMap = new HashMap<>(); public int numSquarefulPerms(int[] A) {
for (int num : A) {
if (!cntMap.containsKey(num)) {
cntMap.put(num, 1);
squareMap.put(num, new HashSet<Integer>());
} else {
cntMap.put(num, cntMap.get(num) + 1);
}
} for (int num1 : cntMap.keySet()) {
for (int num2 : cntMap.keySet()) {
double c = Math.sqrt(num1 + num2);
if (c == Math.floor(c)) {
squareMap.get(num1).add(num2);
squareMap.get(num2).add(num1);
}
}
} for (int key : cntMap.keySet()) {
dfs(key, A.length - 1);
} return ret;
} public void dfs(int key, int left) {
cntMap.put(key, cntMap.get(key)-1);
if (left == 0) ret++;
else {
for (int next : squareMap.get(key)) {
if (cntMap.get(next) != 0) {
dfs(next, left - 1);
}
}
}
cntMap.put(key, cntMap.get(key)+1);
}
}

  

Reference:

https://leetcode.com/problems/number-of-squareful-arrays/discuss/238562/C%2B%2BPython-Backtracking

996. Number of Squareful Arrays的更多相关文章

  1. 【leetcode】996. Number of Squareful Arrays

    题目如下: Given an array A of non-negative integers, the array is squareful if for every pair of adjacen ...

  2. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  3. leetcode 996. Number of Squareful Arrays

    给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...

  4. [Swift]LeetCode996. 正方形数组的数目 | Number of Squareful Arrays

    Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...

  5. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  6. 【LeetCode】回溯法 backtracking(共39题)

    [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. Summary: Arrays vs. Collections && The differences between Collection Interface and Collections Class

    转自http://www.anylogic.com/anylogic/help/index.jsp?topic=/com.xj.anylogic.help/html/code/Arrays_Colle ...

  9. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

随机推荐

  1. void指针及指针的多次赋值的理解

    1.void指针的类型转换 int A::functionCommamd(const DWORD _from,const DWORD _to,const DWORD Event_type,void * ...

  2. 【python3】 解:import导包机制

    模块和包 模块:我们定义的.py结尾的文件就是一个模块,模块中通常定义了类.方法.变量等一系列功能: 包:存放模块的文件夹,含有init.py文件,定义path属性. import语句的作用 impo ...

  3. Leaflet 带箭头轨迹以及沿轨迹带方向的动态marker

    前面写了篇文章,mapboxgl实现带箭头轨迹线,介绍了如何基于mapboxgl实现类似高德地图导航轨迹效果. 下图是我基于leaflet实现的效果. 接下来分享一下在我基于leaflet实现该效果时 ...

  4. 记录mysql查询数据遇到的一个小问题

    今天在测试的时候,需要使用mysql对插入的数据进行检验,但是写完查询语句的时候执行会报错.原因很简单,这个表名是order(订单),在MySQL语言中order是用来排序的关键字,原则上讲是不能作为 ...

  5. unittest系列(一)unittest简介和示例

    unittest - 单元测试框架 单元测试框架是受到 JUnit 的启发,与其他语言中的主流单元测试框架有着相似的风格.其支持测试自动化,配置共享和关机代码测试.支持将测试样例聚合到测试集中,并将测 ...

  6. Scientific Internet Access

    下载小飞机 https://github.com/shadowsocksr-backup 寻找ssr https://github.com/Alvin9999/new-pac/wiki/ss%E5%8 ...

  7. kthread_worker和kthread_work机制

    1.概述 在阅读内核源码时,可以看到kthread_worker.kthread_work两个数据结构配合内核线程创建函数一起使用的场景.刚开始看到这块时,比较困惑,紧接着仔细分析源码后,终于弄清楚了 ...

  8. 以Aliyun体验机为例,从零搭建LNMPR环境(上)

    使用云服务器搭建 Web 运行环境,尤其是搭建常见的 LNMPR(Linux+Nginx+MySQL+PHP+Redis) 环境,对于开发人员是必备的职场基本技能之一.在这里,借着搭建我的" ...

  9. Android Studio 之 用 Drawable resource file 美化 Button 样式

    shape •新建 Drawable resource file 点击 app/src/main/res 找到 drawable 文件夹,右击->New->Drawable Resourc ...

  10. windows平台 cloin +rust 开发环境搭建

    rust 安装请看上一篇 clion 下载地址 破解 教程 1.先执行reset_jetbrains_eval_windows.vbs 2.打开软件选择免费使用 将ide-eval-resetter- ...