此博客中的热门博文
776. Split BST
Problem: Given a Binary Search Tree (BST) with root node root , and a target value V , split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It's not necessarily the case that the tree contains a node with value V . Additionally, most of the structure of the original tree should remain. Formally, for any child C with parent P in the original tree, if they are both in the same subtree after the split, then node C should still have the parent P. You should output the root TreeNode of both subtrees after splitting, in any order. Example 1: Input: root = [4,2,6,1,3,5,7], V = 2 Output: [[2,1],[4,3,6,null,null,5,7]] Explanation: Note that root, output[0], and output[1] are TreeNode objects, not arrays. The given tree [4,2,6,1,3,5,7] is represented by the following diagram: 4 / \ ...
Backpack IV
思路: 马甲题 state: dp[i][j],体积为j时,前i个物品能够占满j体积所需要的方法数。 transition: dp[i][j]+=dp[i-1][j]+dp[i-1][j-k*nums[i-1]], k>=0 && k*nums[i-1]<=j 实现: dp[i][0]需要设置初始值为1,表示体积为0时,前i个物品能取的方法数为1,就是不取。 方法2 : 用一维数组来做dp[j]=dp[j]+dp[j-nums[i-1]],右边的dp[j]表示前i-1个物品能够取体积为j的方法,因为暂时没有更新。实现的时候是nums[i-1]<=j<=tagert,j是从小到大,因为这道题允许物品重复。在j-nums[i-1]>nums[i-1]这种情况下,dp[j-num[i-1]]就会被更新也就是重复的情况。
评论
发表评论