File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } A
3
+ * @return {number }
4
+ */
5
+
6
+ const numSquarefulPerms = function ( A ) {
7
+ const cntMap = { } ;
8
+ const squareMap = { } ;
9
+ let cnt = 0 ;
10
+ for ( let num of A ) {
11
+ if ( ! cntMap . hasOwnProperty ( num ) ) {
12
+ cntMap [ num ] = 1 ;
13
+ squareMap [ num ] = new Set ( ) ;
14
+ } else {
15
+ cntMap [ num ] = cntMap [ num ] + 1 ;
16
+ }
17
+ }
18
+
19
+ for ( let num1 of Object . keys ( cntMap ) ) {
20
+ for ( let num2 of Object . keys ( cntMap ) ) {
21
+ let c = Math . sqrt ( + num1 + + num2 ) ;
22
+ if ( c === Math . floor ( c ) ) {
23
+ squareMap [ num1 ] . add ( + num2 ) ;
24
+ squareMap [ num2 ] . add ( + num1 ) ;
25
+ }
26
+ }
27
+ }
28
+ for ( let num of Object . keys ( cntMap ) ) {
29
+ countPerm ( num , A . length - 1 ) ;
30
+ }
31
+ return cnt ;
32
+ function countPerm ( num , left ) {
33
+ cntMap [ num ] = cntMap [ num ] - 1 ;
34
+ if ( left === 0 ) {
35
+ cnt ++ ;
36
+ } else {
37
+ for ( let next of squareMap [ num ] ) {
38
+ if ( cntMap [ next ] !== 0 ) {
39
+ countPerm ( next , left - 1 ) ;
40
+ }
41
+ }
42
+ }
43
+ cntMap [ num ] = cntMap [ num ] + 1 ;
44
+ }
45
+ } ;
You can’t perform that action at this time.
0 commit comments