国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[Leetcode - Dynamic Programming] Partition Equal S

qpal / 3234人閱讀

摘要:背包問題假設(shè)有個寶石,只有一個容量為的背包,且第個寶石所對應(yīng)的重量和價值為和求裝哪些寶石可以獲得最大的價值收益思路我們將個寶石進(jìn)行編號,尋找的狀態(tài)和狀態(tài)轉(zhuǎn)移方程。我們用表示將前個寶石裝到剩余容量為的背包中,那么久很容易得到狀態(tài)轉(zhuǎn)移方程了。

Partition Equal Subset Sum

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.
Example 1:

Input: [1, 5, 11, 5]

Output: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.

1.解題思路
此問題屬于動態(tài)規(guī)劃中的背包問題。
背包問題:假設(shè)有n個寶石,只有一個容量為C的背包,且第i個寶石所對應(yīng)的重量和價值為w[i]和v[i],求裝哪些寶石可以獲得最大的價值收益?
思路:我們將n個寶石進(jìn)行編號,0,1,2...n-1,尋找DP的狀態(tài)和狀態(tài)轉(zhuǎn)移方程。我們用dpij表示將前i個寶石裝到剩余容量為j的背包中,那么久很容易得到狀態(tài)轉(zhuǎn)移方程了。(寶石從0開始編號,所以dpij是在考慮第i-1個寶石裝包的情況,當(dāng)然我們要先初始化前0個寶石裝包的情況,即dp0=0,因為不裝任何寶石,所以無論如何價值都為0.)

dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-w[i-1]]+v[i-1])
背包無法再裝下第i-1個寶石-> dp[i-1][j];
繼續(xù)將第i-1個寶石裝包->  dp[i-1][j-w[i-1]]+v[i-1]。

搞清楚了背包問題,這個Partition Equal Subset Sum的題目就迎刃而解了。
1). 判斷數(shù)組中所有數(shù)的和是否為偶數(shù),因為奇數(shù)是不可能有解的;
2). 根據(jù)背包問題,取前i個數(shù),體積為j的情況下,

dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-nums[i-1]]+nums[i-1])

3).如果最后dpnums.length=sum/2,則返回true.

2.代碼

public class Solution {
    public boolean canPartition(int[] nums) {
        if(nums.length==0) return false;
        int sum=0;
        for(int n:nums){
            sum+=n;
        }
        if(sum%2==1) return false;
        sum=sum/2;
        int[][] dp=new int[nums.length+1][sum+1];
        for(int i=0;i<=nums.length;i++){
            for(int j=0;j<=sum;j++){
                if(i==0) //表示前0個數(shù),所以價值均為0;
                    dp[i][j]=0;
                //在裝第i-1個數(shù)時,先判斷剩余容量j是否大于nums[i-1]
                else if(j           
               
                                           
                       
                 

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/66252.html

相關(guān)文章

  • [LeetCode] 698. Partition to K Equal Sum Subsets

    Problem Given an array of integers nums and a positive integer k, find whether its possible to divide this array into k non-empty subsets whose sums are all equal. Example 1:Input: nums = [4, 3, 2, 3,...

    kuangcaibao 評論0 收藏0
  • [LeetCode] 663. Equal Tree Partition

    Problem Given a binary tree with n nodes, your task is to check if its possible to partition the tree to two trees which have the equal sum of values after removing exactly one edge on the original tr...

    coordinate35 評論0 收藏0
  • [Leetcode-Dynamic Programming]Unique Binary Search

    Unique Binary Search TreesGiven n, how many structurally unique BSTs (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BSTs. 1 3 3 ...

    MartinDai 評論0 收藏0
  • leetcode416. Partition Equal Subset Sum

    摘要:題目要求假設(shè)有一個全為正整數(shù)的非空數(shù)組,將其中的數(shù)字分為兩部分,確保兩部分?jǐn)?shù)字的和相等。而這里的問題等價于,有個物品,每個物品承重為,問如何挑選物品,使得背包的承重搞好為所有物品重量和的一般。 題目要求 Given a non-empty array containing only positive integers, find if the array can be partitio...

    Caicloud 評論0 收藏0
  • [LeetCode] 416. Partition Equal Subset Sum

    Problem Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note:Each of the array ...

    makeFoxPlay 評論0 收藏0

發(fā)表評論

0條評論

qpal

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<