996. Number of Squareful Arrays
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
andA2
differ if and only if there is some indexi
such thatA1[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 <= A.length <= 12
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的更多相关文章
- 【leetcode】996. Number of Squareful Arrays
题目如下: Given an array A of non-negative integers, the array is squareful if for every pair of adjacen ...
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- leetcode 996. Number of Squareful Arrays
给定一个长度小于 12 的数组 要求排列方式的种数 使得相邻和为完全平方 不考虑数学结构 将问题转化为 一笔画问题 和为完全平方代表 之间存在通路 回溯法 N^N 记忆化搜索 NN 2^N 判断是否是 ...
- [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 ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- 【LeetCode】回溯法 backtracking(共39题)
[10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses ( ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 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 ...
- 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 ...
随机推荐
- 第43天学习打卡(JVM探究)
JVM探究 请你谈谈你对JVM的理解?Java8虚拟机和之前的变化更新? 什么是OOM,什么是栈溢出StackOverFlowError? 怎么分析? JVM的常用调优参数有哪些? 内存快照如何抓取, ...
- JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- Wayland architecture
Introduction Motivation Most Linux and Unix-based systems rely on the X Window System (or simply X) ...
- Python3.x 基础练习题100例(21-30)
练习21: 题目: 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前 一天剩下的一半零一个.到第10天早上 ...
- xmake v2.5.2 发布, 支持自动拉取交叉工具链和依赖包集成
xmake 是一个基于 Lua 的轻量级跨平台构建工具,使用 xmake.lua 维护项目构建,相比 makefile/CMakeLists.txt,配置语法更加简洁直观,对新手非常友好,短时间内就能 ...
- LeetCode1576. 替换所有的问号
原题链接 1 class Solution { 2 public: 3 string modifyString(string s) { 4 int lens = s.length(); 5 for(i ...
- eureka server 配置
启动类 import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure. ...
- pytorch(07)数据模型的读取
DataLoader与Dataset pytorch中的数据读取机制 graph TB DataLoader --> DataLoaderIter DataLoaderIter --> S ...
- PCA——主成分分析
PCA(Principal Components Analysis)主成分分析是一个简单的机器学习算法,利用正交变换把由线性相关变量表示的观测数据转换为由少量线性无关比变量表示的数据,实现降维的同时尽 ...
- 【python+selenium的web自动化】- 8种元素定位方式详解
我们在做WEB自动化时,最根本的就是操作页面上的各种元素,而操作的基础便是元素的定位,只有准确地定位到唯一元素才能进行后续的自动化控制,下面将对各种元素定位方式进行总结归纳. 说明:以下操作统 ...