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. # PyComCAD介绍及开发方法

    项目地址:https://github.com/JohnYang1210/PycomCAD 1.综述 ​ 提到Autocad在工业界的二次开发,VB或者Lisp可能作为常用的传统的编程语言.但是,Py ...

  2. Java网络编程UDP通信原理

    前言 继续今天我们的Java网络编程--TCP和UDP通信 一.TCP和UDP概述 传输层通常以TCP和UDP协议来控制端点与端点的通信   TCP UDP 协议名称 传输控制协议 用户数据包协议 是 ...

  3. LightOJ-1074(SPFA判负圈+Bellman-Ford算法)

    Extended Traffic LightOJ-1074 这题因为涉及到减法和三次方,所以可能会出现负圈. 这里使用的算法叫做SPFA算法,这个可以用来判负圈和求解最短路.Bellman-Ford算 ...

  4. 冗余网络构建方案对比:VRRP协议、多网卡绑定及WN202冗余链路网卡

    在组建网络时为网络设计冗余方案已经成为提高网络可用性必不可少的一环,伴随着网络技术的发展实现网络冗余的技术方案也是层出不穷,例如应用于服务器端的HA.LB,应用于存储的SAN.DAS.NAS等.本文重 ...

  5. External Libraries中没有Maven的jar包的原因(已解决)

    **深坑!** ## External Libraries中没有Maven的jar包的原因(已解决) 2021年3月1日 --- 搭建一个新项目 IDEA 从 Git 上拉 拉去Maven项目然后 m ...

  6. 04-Spring自定义标签解析

    自定义标签的解析 这一篇主要说明自定义标签的解析流程,除了 bean.alias.import.beans之外的标签,都属于自定义标签的范围,自定义标签的解析需要命名空间配合, 获取对应的命名空间 根 ...

  7. MySQL入门(7)——表数据的增、删、改

    MySQL入门(7)--表数据的增.删.改 插入数据 使用INSERT···VALUES语句插入数据 INSERT语句最常用的格式是INSERT···VALUES: INSERT [LOW_PRIOR ...

  8. ES6学习笔记(2)- 箭头函数

    1. 箭头函数声明 箭头函数的声明方式示例: 1 const printValue = (condition) => { 2 let testValue = 55; 3 if (conditio ...

  9. beego框架panic: 'GetSecurityInf' method doesn't exist in the controller CorporateInfcontroller问题解决

    在使用beego框架时,出现类似于panic: 'GetSecurityInf' method doesn't exist in the controller CorporateInfcontroll ...

  10. python之模块与类库

    什么是模块 模块是一组类,函数,方法所组成的.这些类都储存在文本文件中..py是python程序代码中的扩展名,模块可能是c或者python写的.模块的扩展名可以是.py或者是.pyc(经过编译的.p ...