daily pastebin goal
73%
SHARE
TWEET

Untitled

a guest Feb 16th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2.    
  3.     boolean perfSum(int val){
  4.         int sqrt = (int)Math.sqrt(val);
  5.         return sqrt*sqrt == val;
  6.     }
  7.    
  8.     public int numSquarefulPerms(int[] a) {        
  9.         return count(a, -1, 0);
  10.     }
  11.    
  12.     int count(int[] a, int prevIndex, int visited){
  13.         if(visited == (1<<(a.length)) - 1){
  14.             return 1;
  15.         }else{                            
  16.             int result = 0;
  17.             Set<Integer> checked = new HashSet<>();
  18.            for(int i=0;i<a.length;++i){              
  19.                if((visited&(1<<i))==0 && !checked.contains(a[i])&& (prevIndex==-1 || perfSum(a[prevIndex] + a[i]))){                                            
  20.                         checked.add(a[i]);                          
  21.                         result += count(a, i, visited|(1<<i));                                                                      
  22.                }
  23.            }
  24.            
  25.             return result;
  26.         }
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top