dynamic-programming

Knapsack with minimum cost

与世无争的帅哥 提交于 2019-12-21 06:42:31
问题 I've got several types of coins, each have weight (wi) and cost (ci). So I've got to make a knapsack with weight==W and (!) minimum cost of coins in it. I can`t make recurrence relation to use DP. 回答1: Just formulate the usual recurrence relation... Designate the minimum cost achievable with total weight k as Min_cost(k). Bootstrap the memoization with: Min_cost(0) = cost of empty set = 0 Then solve for increasing values of k using: Min_cost(i+1) = min [Min_cost(i) + min [ci, for all items

box stacking problem

懵懂的女人 提交于 2019-12-21 05:38:06
问题 Given are n boxes in three dimension ( h , w , d ). The goal is to stack them on top of each other to have a maximum height (boxes can be rotated). Each box that you put on top should have a smaller dimension ( w , d ) than the one below. How can we do it with dynamic programming and greedy? 回答1: This is the box stacking problem - problem 4 there. If you want to think about it yourself, think about how you can adapt the longest increasing subsequence algorithm for solving this. 来源: https:/

What's the best way to implement a dynamic proxy in C#?

三世轮回 提交于 2019-12-21 05:31:23
问题 I've got a need to create a dynamic proxy in C#. I want this class to wrap another class, and take on it's public interface, forwarding calls for those functions: class MyRootClass { public virtual void Foo() { Console.Out.WriteLine("Foo!"); } } interface ISecondaryInterface { void Bar(); } class Wrapper<T> : ISecondaryInterface where T: MyRootClass { public Wrapper(T otherObj) { } public void Bar() { Console.Out.WriteLine("Bar!"); } } Here's how I want to use it: Wrapper<MyRootClass>

False Mirrors. can you help me to solve?

旧时模样 提交于 2019-12-21 05:21:09
问题 Here is the problem BFG-9000 destroys three adjacent balconies per one shoot. (N-th balcony is adjacent to the first one). After the shoot the survival monsters inflict damage to Leonid (main hero of the novel) — one unit per monster. Further follows new shoot and so on until all monsters will perish. It is required to define the minimum amount of damage, which can take Leonid. For example : N = 8 A[] = 4 5 6 5 4 5 6 5 answer : 33 4 * * * 4 5 6 5 - 24 4 * * * * * * 5 - 9 * * * * * * * * - 0

False Mirrors. can you help me to solve?

不羁的心 提交于 2019-12-21 05:21:06
问题 Here is the problem BFG-9000 destroys three adjacent balconies per one shoot. (N-th balcony is adjacent to the first one). After the shoot the survival monsters inflict damage to Leonid (main hero of the novel) — one unit per monster. Further follows new shoot and so on until all monsters will perish. It is required to define the minimum amount of damage, which can take Leonid. For example : N = 8 A[] = 4 5 6 5 4 5 6 5 answer : 33 4 * * * 4 5 6 5 - 24 4 * * * * * * 5 - 9 * * * * * * * * - 0

Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)?

大憨熊 提交于 2019-12-21 04:00:34
问题 The question is to find out sum of distances between every two nodes of BinarySearchTree given that every parent-child pair is separated by unit distance. It is to be calculated after every insertion. ex: ->first node is inserted.. (root) total sum=0; ->left and right node are inserted (root) / \ (left) (right) total sum = distance(root,left)+distance(root,right)+distance(left,right); = 1 + 1 + 2 = 4 and so on..... Solutions I came up with: Brute-force. Steps: perform a DFS and track all the

How to solve 5 * 5 Cube in efficient easy way

半世苍凉 提交于 2019-12-21 02:04:51
问题 There is 5*5 cube puzzle named Happy cube Problem where for given mat , need to make a cube . http://www.mathematische-basteleien.de/cube_its.htm#top Its like, 6 blue mats are given- From the following mats, Need to derive a Cube - These way it has 3 more solutions. So like first cub For such problem, the easiest approach I could imagine was Recursion based where for each cube, I have 6 position , and for each position I will try check all other mate and which fit, I will go again recursively

Dynamic programming and Divide and conquer

寵の児 提交于 2019-12-20 23:26:00
问题 I was reading notes on Dynamic programming, and I encountered the following comment. If the subproblems are not independent, i.e. subproblems share subsubproblems, then a divideand-conquer algorithm repeatedly solves the common subsubproblems. Thus, it does more work than necessary What does this mean ? Can you give me examples to make the above point clear ? 回答1: The author refers to the fact that many divide-and-conquer algorithms have subproblems that overlap with one another. Consider,

Dynamic programming and Divide and conquer

我是研究僧i 提交于 2019-12-20 23:25:32
问题 I was reading notes on Dynamic programming, and I encountered the following comment. If the subproblems are not independent, i.e. subproblems share subsubproblems, then a divideand-conquer algorithm repeatedly solves the common subsubproblems. Thus, it does more work than necessary What does this mean ? Can you give me examples to make the above point clear ? 回答1: The author refers to the fact that many divide-and-conquer algorithms have subproblems that overlap with one another. Consider,

find a solution to subset sum using dynamic programming

我与影子孤独终老i 提交于 2019-12-20 14:41:33
问题 What I want to do I want to find a subset of an array that sums to a target T . I also want to use to a dynamic programming approach (and a bottom-up solution at that) to do this. What I currently have Currently I only found a way to see if amongst all subsets of size N , whether or not there is at least one subset that has the desired sum. See code below. public boolean solve(int[] numbers, int target) { //Safeguard against invalid parameters if ((target < 0) || (sum(numbers) < target)){