996. Number of Squareful Arrays
Given an array
Aof 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
A1andA2differ if and only if there is some indexisuch 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 <= 120 <= 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 ...
随机推荐
- Prism.WPF -- Prism框架使用(下)
本文参考Prism官方示例 命令使用 Prism提供了两种命令:DelegateCommand和CompositeCommand. DelegateCommand DelegateCommand封装了 ...
- Java RPC 框架 Solon 1.3.7 发布,增强Cloud接口能力范围
Solon 是一个微型的Java RPC开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,4000多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:RPC. ...
- Shiro反序列化漏洞检测、dnslog
目录 信息收集 poc 参考 信息收集 poc # pip install pycrypto import sys import base64 import uuid from random impo ...
- python+unittest+ddt数据驱动进行接口自动化测试
所谓数据驱动测试,简单的理解为数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变.通过使用数据驱动测试的方法,可以在需要验证多组数据测试场景中,使用外部数据源实现对输入输出与期望值的参数化,避 ...
- Github Fork与远程主分支同步
fork与主分支同步(5步) 1. git remote add upstream git@github.com:haichong98/gistandard.git 新建一个upstream的远程 ...
- 漏洞复现-2.x rce-Thinkphp远程命令执行
0x00实验环境 攻击机:win10 靶机:Ubuntu18 (docker搭建的vulhub靶场) 0x01影响版本 影响Thinkphp 2.x的版本 0x02实验目的 学 ...
- 如何优雅的移植JavaScript组件到Blazor
Blazor作为一个新兴的交互式 Web UI 的框架,有其自身的优缺点,如果现有的 JavaScript 组件能移植到 Blazor,无疑让 Blazor 如虎添翼,本文就介绍一下自己在开发 Bul ...
- 基于renren-fast的快速入门项目实战(实现报表增删改查)
基于renren-fast的快速入门项目实战(实现报表增删改查) 说明:renren-fast是一个开源的基于springboot的前后端分离手脚架,当前版本是3.0 官方开发文档需付费,对于新手而言 ...
- Java 树结构实际应用 二(哈夫曼树和哈夫曼编码)
赫夫曼树 1 基本介绍 1) 给定 n 个权值作为 n 个叶子结点,构造一棵二叉树,若该树的带权路径长度(wpl)达到最小,称这样的二叉树为 最优二叉树,也称为哈夫曼树(Huffman Tree), ...
- 【LeetCode】2020-04 每日一题
8. 字符串转换整数 (atoi)(中等) [分类]:模拟.正则表达式 [题解]: 解法1:直接模拟,但是在判断INT_MAX和INT_MIN上需要注意,因为直接判断会超出范围,所以可以将式子转换一下 ...