- 題目:將一個數組分成兩部分,不要求兩部分所包含的元素個數相等,要求使得這兩個部分的和的差值最小。比如對於數組{1,0,1,7,2,4},可以分成{1,0,1,2,4}和{7},使得這兩部分的差值最小。
思路:這個問題可以轉化為求數組的一個子集,使得這個子集中的元素的和盡可能接近sum/2,其中sum為數組中所有元素的和。這樣轉換之后這個問題就很類似0-1背包問題了:在n件物品中找到m件物品,他們的可以裝入背包中,且總價值最大不過這里不考慮價值,就考慮使得這些元素的和盡量接近sum/2。
下面列狀態方程:
dp[i][j]表示前i件物品中,總和最接近j的所有物品的總和,其中包括兩種情況:
- 第i件物品沒有包括在其中
- 第i件物品包括在其中
如果第i件物品沒有包括在其中,則dp[i][j] = dp[i-1][j]
如果第i件物品包括在其中,則dp[i][j] = dp[i-1][j-vec[i]]
當然,這里要確保j-vec[i] >= 0。
所以狀態轉移方程為:
dp[i][j] = max(dp[i-1][j],dp[i-1][j-vec[i]]+vec[i]);
for (int i = 1; i <= len; ++i) { for (int j = 1; j <= sum / 2; ++j) { if(j>=vec[i-1]) dp[i][j] = max(dp[i-1][j],dp[i-1][j-vec[i-1]]+vec[i-1]); else dp[i][j] = dp[i - 1][j]; } }
-
Leetcode學習—— Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. 給出一個長度為 2n 的整數數組,你的任務是將這些整數分成n組,每組兩個一對,並求得 所有分組中較小的數 的總和(這個總和的值要盡可能的大) Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4. Note: n is a positive integer, which is in the range of [1, 10000]. All the integers in the array will be in the range of [-10000, 10000]. 思路:將整個數組升序排列,從下標為 0 處開始,每隔兩個 取一個,並求和 class Solution(object): def arrayPartitionI(self, nums): return sum(sorted(nums)[::2])