Skip to content

Commit 4f2be83

Browse files
committed
996. Number of Squareful Arrays
1 parent 6580185 commit 4f2be83

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

number-of-squareful-arrays.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)