We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6580185 commit 4f2be83Copy full SHA for 4f2be83
number-of-squareful-arrays.cpp
@@ -0,0 +1,41 @@
1
+// Runtime: 4 ms
2
+// Memory Usage: 8.9 MB
3
+class Solution {
4
+public:
5
+ map<int, int> count;
6
+ map<int, vector<int> > adj;
7
+ int res = 0;
8
+
9
+ void dfs(int src, int left) {
10
+ count[src]--;
11
+ if (!left) res++;
12
13
+ for (int a : adj[src]) {
14
+ if (count[a]) {
15
+ dfs(a, left - 1);
16
+ }
17
18
+ count[src]++;
19
20
21
+ int numSquarefulPerms(vector<int>& A) {
22
+ for (int a : A) {
23
+ count[a]++;
24
25
26
+ for (auto a : count) {
27
+ for (auto b : count) {
28
+ int sum = a.first + b.first;
29
+ double srt = sqrt(sum);
30
+ if ((srt - floor(srt)) == 0) {
31
+ adj[a.first].push_back(b.first);
32
33
34
35
36
+ for (auto a : adj) {
37
+ dfs(a.first, A.size() - 1);
38
39
+ return res;
40
41
+};
0 commit comments