LeetCode 996.正方形数组的数目

和上一道状压的区别在于我们要去重一下~

思路都是和上一篇博客是一样的,感兴趣的同学可以看一下

cpp 复制代码
const int N = 15;
int dp[1<<N][N];
int n;
vector<int>nums1;

bool check(int x){
    int tem  = sqrt(x);
    if(tem*tem==x)return 1;
    return 0;
}


int dfs(int u,int id){
    if(u==0)return 1;
    if(~dp[u][id])return dp[u][id];

    int res = 0;
    for(int i=0;i<n;i++){
        if(((u>>i)&1) && check(nums1[i]+nums1[id]))
          res = res + dfs(u&~(1<<i),i);
    }

    return dp[u][id] = res;
}


class Solution {
public:
    int numSquarefulPerms(vector<int>& nums) {
        n = nums.size();
        nums1 = nums;
        memset(dp,-1,sizeof dp);
        int ans = 0;
        int u = (1<<n)-1;
        for(int i=0;i<n;i++)
         ans = ans + dfs(u&~(1<<i),i);
        

        map<int,int>mp;
        for(auto &t:nums)++mp[t];

        for(auto [_,cnt]:mp){
            while(cnt){
                ans/=cnt;
                cnt--;
            }
        }



        return ans;

    }
};
相关推荐
oioihoii15 分钟前
C++随机打乱函数:简化源码与原理深度剖析
开发语言·c++·算法
不知名。。。。。。。。29 分钟前
分治算法---快排
算法
minji...1 小时前
数据结构 算法复杂度(1)
c语言·开发语言·数据结构·算法
凌肖战1 小时前
力扣网编程150题:加油站(贪心解法)
算法·leetcode·职场和发展
吃着火锅x唱着歌1 小时前
LeetCode 3306.元音辅音字符串计数2
算法·leetcode·c#
kngines1 小时前
【力扣(LeetCode)】数据挖掘面试题0003: 356. 直线镜像
leetcode·数据挖掘·直线镜像·对称轴
不見星空1 小时前
【leetcode】1751. 最多可以参加的会议数目 II
算法·leetcode
不見星空1 小时前
leetcode 每日一题 3439. 重新安排会议得到最多空余时间 I
算法·leetcode
SsummerC1 小时前
【leetcode100】下一个排列
python·算法·leetcode
black_blank1 小时前
st表 && csp37 第四题 集体锻炼
java·数据结构·算法