Balanced Partition of Array

本文介绍了一种使用动态规划解决将数组分成两部分并使这两部分的和之差最小的问题的方法。通过构建布尔型动态规划表格,实现了寻找能使两部分和最接近的子序列的目标。

Given an array which contains random positive integers, divide the array into two parts which has the smallest diff sum, return the smallest diff sum.

This problem is solvable using dynamic programming. Using s(i, j) to stands for "there is a sum(1, i) to j" Thus, s(i, j) = s(i-1, j) || s(i, j - nums[i]);

This goes back to "coins exchange or backpacking problem again". To get the minimum difference, we can check whether there is any subsequence equals to (Sum(i, n) / 2 ... 0)

#include "header.h"
using namespace std;

// partition an array into two parts so that the two parts has mini differnce of sum.
// the two array length might not be even.

int miniDiff(vector<int>& nums) {
  int sum = 0;
  for(int i = 0; i < nums.size(); ++i) {
    sum += nums[i];
  }
  int size = nums.size();
  vector< vector<bool> > dp(size + 1, vector<bool>(sum + 1, false));
  for(int i = 0; i <= size; ++i) {
    dp[i][0] = true;
  }
  for(int i = 1; i <= sum; ++i) {
    dp[0][i] = false;
  }

  for(int i = 1; i <= size; ++i) {
    for(int j = 1; j <= sum; ++j) {
      if(j - nums[i - 1] >= 0) {
        dp[i][j] = (dp[i - 1][j] || dp[i][j - nums[i - 1]]);
      } else dp[i][j] = dp[i-1][j];
    }
  }

  for(int i = 1; i <= size; ++i) {
    for(int j = sum / 2; j >= 0; --j) {
      if(dp[i][j]) return sum - j - j;
    }
  }
  return -1;
}

int main(void) {
  vector<int> nums{1, 2, 4, 4};
  cout << miniDiff(nums) << endl;
}

Follow Up, what if the two divided arrays has length diff with no more than 1, return the minimum difference sum. Question


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值