LeetCode 2035. 将数组分成两个数组并最小化数组和的差(状态压缩DP)

文章目录

1. 题目

给你一个长度为 2 * n 的整数数组。
你需要将 nums 分成 两个 长度为 n 的数组,分别求出两个数组的和,并 最小化 两个数组和之 差的绝对值 。
nums 中每个元素都需要放入两个数组之一。

请你返回 最小数组和之差

示例 1:
输入:nums = [3,9,7,3]
输出:2
解释:最优分组方案是分成 [3,9][7,3] 。
数组和之差的绝对值为 abs((3 + 9) - (7 + 3)) = 2 。

示例 2:
输入:nums = [-36,36]
输出:72
解释:最优分组方案是分成 [-36][36] 。
数组和之差的绝对值为 abs((-36) - (36)) = 72 。

示例 3:
输入:nums = [2,-1,0,4,-2,-9]
输出:0
解释:最优分组方案是分成 [2,4,-9][-1,0,-2] 。
数组和之差的绝对值为 abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0 。
 
提示:
1 <= n <= 15
nums.length == 2 * n
-10^7 <= nums[i] <= 10^7

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/partition-array-into-two-arrays-to-minimize-sum-difference
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 数组折半,分别对一半进行状态枚举
  • 枚举一边取的数的个数,将左右的满足二进制位个数的状态取出,排序,双指针求解最接近的
  • 时间复杂度 O ( n ∗ 2 n ) O(n*2^n) O(n2n)
class Solution {
public:
    int minimumDifference(vector<int>& nums) {
        int n = nums.size()/2;
        int sum = accumulate(nums.begin(), nums.end(), 0);
        vector<int> a(nums.begin(),nums.begin()+n), b(nums.begin()+n,nums.end());
        vector<int> stateSum1 = getsum(a);//获取所有状态的和
        vector<int> stateSum2 = getsum(b);
        int dis = INT_MAX;
        for(int x = 0; x <= n; ++x)
        { // 第一个数组取x个数
            int y = n-x; // 第二个数组取y个数
            vector<int> s1, s2;//把两边取出来的和存储
            for(int state = 0; state < (1<<n); ++state)
            {
                if(count1(state)==x)
                    s1.push_back(stateSum1[state]);
            }
            for(int state = 0; state < (1<<n); ++state)
            {
                if(count1(state)==y)
                    s2.push_back(stateSum2[state]);
            }
            sort(s1.begin(), s1.end());//排序,双指针,求解最接近的
            sort(s2.begin(), s2.end());
            int len1 = s1.size(), len2 = s2.size();
            int i = 0, j = len2-1;
            while(i < len1 && j >= 0)
            {
                dis = min(dis, abs(sum-2*(s1[i]+s2[j])));
                if(s1[i]+s2[j] > sum-(s1[i]+s2[j]))
                    j--;
                else if(s1[i]+s2[j] < sum-(s1[i]+s2[j]))
                    i++;
                else
                    return 0;
            }
        }
        return dis;
    }
    vector<int> getsum(vector<int>& num)
    {
        int n = num.size();
        vector<int> ans(1<<n);
        for(int i = 0; i < n; ++i)
        {
            ans[1<<i] = num[i]; // 初始化
        }
        for(int state = 1; state < (1<<n); ++state)
        {
            int prevstate = state&(state-1);
            ans[state] = ans[state-prevstate] + ans[prevstate];
        }
        return ans;
    }
    int count1(int x)
    { // 计算二进制1的个数
        int ans = 0;
        while(x)
        {
            ans++;
            x = x&(x-1);
        }
        return ans;
    }
};

692 ms 76.4 MB C++


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Michael阿明

如果可以,请点赞留言支持我哦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值