dynamic-programming

how to convert a string into a palindrome with minimum number of operations?

↘锁芯ラ 提交于 2019-12-03 11:42:49
问题 Here is the problem states to convert a string into a palindrome with minimum number of operations. I know it is similar to the Levenshtein distance but I can't solve it yet For example, for input mohammadsajjadhossain , the output is 8 . 回答1: Perform Levenshtein distance on the string and its reverse. The solution will be the minimum of the operations in the diagonal of the DP array going from bottom-left to top-right, as well as each entry just above and just below the diagonal. This works

counting boolean parenthesizations implementation

♀尐吖头ヾ 提交于 2019-12-03 09:32:11
问题 Given a boolean expression containing the symbols {true, false, and, or, xor}, count the number of ways to parenthesize the expression such that it evaluates to true. For example, there is only 1 way to parenthesize 'true and false xor true' such that it evaluates to true. Here is my algorithm we can calculate the total number of parenthesization of a string Definition: N - the total number of True - the number of parenthesizations that evaluates to true False - the number of

efficient longest common subsequence algorithm library?

主宰稳场 提交于 2019-12-03 09:14:28
问题 I'm looking for a (space) efficient implementation of an LCS algorithm for use in a C++ program. Inputs are two random access sequences of integers. I'm currently using the dynamic programming approach from the wikipedia page about LCS. However, that has O(mn) behaviour in memory and time and dies on me with out of memory errors for larger inputs. I have read about Hirschberg's algorithm, which improves memory usage considerably, Hunt-Szymanski and Masek and Paterson. Since it isn't trivial

Minimum steps to set a series of rotating dials to a given sequence

可紊 提交于 2019-12-03 07:54:26
问题 So I have the following problem: There are n rotating dials each set to some number between 0-9 and they need to be matched with another series of n numbers(also between 0-9). One step of rotation is rotating any number of consecutive dials up or down by one step. The dials wrap around 9. i.e. rotating one step up from 9 gives 0 and vice versa. I need to find the minimum number of steps to match the initial configuration with the given configuration. Ex: Initial -> 154 Given -> 562 1. first

Knapsack with continuous (non distinct) constraint

浪子不回头ぞ 提交于 2019-12-03 07:42:02
问题 I watched Dynamic Programming - Kapsack Problem (YouTube). However, I am solving a slightly different problem where the constraint is the budget, price, in double, not integer. So I am wondering how can I modify that? Double is "continuous" unlike integer where I can have 1,2,3 .... I don't suppose I do 0.0, 0.1, 0.2 ...? UPDATE 1 I thought of converting double to int by multiply by 100. Money is only 2 decimal places. But that will mean the range of values will be very large? UPDATE 2 The

Dynamic programming and Divide and conquer

大城市里の小女人 提交于 2019-12-03 07:18:06
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 ? The author refers to the fact that many divide-and-conquer algorithms have subproblems that overlap with one another. Consider, for example, this very simple Fibonacci implementation: int Fibonacci(int n) { if (n <= 1) return n; return

Algorithm for solving this distributing beads puzzle?

你说的曾经没有我的故事 提交于 2019-12-03 06:56:38
Lets say you have a circle (like below) with N spots, and you have N beads distributed in the slots. Here's an example: Each bead can be moved clockwise for X slots, which costs X^2 dollars. Your goal is to end up with one bead in each slot. What is the minimum amount of money you have to spend to achieve this task? More interesting variation of this problem: Algorithm for distributing beads puzzle (2)? In this answer I assume beads can only be moved once. Otherwise it would be evident that beads should only move one square at a time, which makes this problem much less interesting: the sum of

What is the “cut-and-paste” proof technique?

旧时模样 提交于 2019-12-03 06:38:34
问题 I've seen references to cut-and-paste proofs in certain texts on algorithms analysis and design. It is often mentioned within the context of Dynamic Programming when proving optimal substructure for an optimization problem (See Chapter 15.3 CLRS). It also shows up on graphs manipulation. What is the main idea of such proofs? How do I go about using them to prove the correctness of an algorithm or the convenience of a particular approach? 回答1: The term "cut and paste" shows up in algorithms

Two player grid traversal game

瘦欲@ 提交于 2019-12-03 06:23:10
问题 Given a M * N grid and location of two players p1 and p2 on grid. There are n balls placed on different positions on the grid. Let the location of these balls be B(1), B(2), B(3) ..., B(n) . We need to calculate the minumum manhattan distance required to pick all the balls. Balls should be picked in ascending order i.e if B(i) is picked before B(j) if i < j . Consider the following sample case: p1 = (1, 1) p2 = (3, 4) Lets consider location of balls as B(1) = (1, 1), B(2) = (2, 1), B(3) = (3,

Memoization or Tabulation approach for Dynamic programming

不羁岁月 提交于 2019-12-03 04:16:56
问题 There are many problems that can be solved using Dynamic programming e.g. Longest increasing subsequence. This problem can be solved by using 2 approaches Memoization (Top Down) - Using recursion to solve the sub-problem and storing the result in some hash table. Tabulation (Bottom Up) - Using Iterative approach to solve the problem by solving the smaller sub-problems first and then using it during the execution of bigger problem. My question is which is better approach in terms of time and