dynamic-programming

How to Determine Cheapest Commute Ticket Combination

孤者浪人 提交于 2019-12-04 13:50:18
My local train service recently added an option for dialy commute. I am trying to determine the algorithm for finding the cheapest combination of tickets for a given set of round trips on given days. Here is the problem in english. Given a set of days and and rides per day what combination of the following is the cheapest. A round trip ticket at cost w per round trip. A 7 day ticket at cost x for unlimited rides during 7 consecutive calendar days. A 30 day ticket at cost y for unlimited rides during 30 consecutive calendar days. A 365 day ticket at cost z for unlimited rides during 365

How many possible scorecards are consistent with the input scorecard?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 13:44:47
I have been trying to solve the following problem in interview street. Count Scorecards(30 points) In a tournament, N players play against each other exactly once. Each game results in either of the player winning. There are no ties. You have given a scorecard containing the scores of each player at the end of the tournament. The score of a player is the total number of games the player won in the tournament. However, the scores of some players might have been erased from the scorecard. How many possible scorecards are consistent with the input scorecard? Input: The first line contains the

Maximum sum of the range non-overlapping intervals in a list of Intervals

半世苍凉 提交于 2019-12-04 13:35:11
问题 Someone asked me this question: You are given a list of intervals. You have to design an algorithm to find the sequence of non-overlapping intervals so that the sum of the range of intervals is maximum. For Example: If given intervals are: ["06:00","08:30"], ["09:00","11:00"], ["08:00","09:00"], ["09:00","11:30"], ["10:30","14:00"], ["12:00","14:00"] Range is maximized when three intervals [“06:00”, “08:30”], [“09:00”, “11:30”], [“12:00”, “14:00”], are chosen. Therefore, the answer is 420

0-1 Knapsack algorithm

百般思念 提交于 2019-12-04 11:51:06
问题 Is the following 0-1 Knapsack problem solvable: 'float' positive values and 'float' weights (can be positive or negative) 'float' capacity of the knapsack > 0 I have on average < 10 items, so I'm thinking of using a brute force implementation. However, I was wondering if there is a better way of doing it. 回答1: This is a relatively simple binary program. I'd suggest brute force with pruning. If at any time you exceed the allowable weight, you don't need to try combinations of additional items,

Algorithm for solving this distributing beads puzzle?

落花浮王杯 提交于 2019-12-04 11:16:53
问题 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)? 回答1: In this answer I assume beads can only be moved once. Otherwise it would be evident

How to implement a dynamic programming algorithms to TSP in Python?

吃可爱长大的小学妹 提交于 2019-12-04 10:10:17
I want to solve the TSP problem using a dynamic programming algorithm in Python.The problem is: Input: cities represented as a list of points. For example, [(1,2), (0.3, 4.5), (9, 3)...]. The distance between cities is defined as the Euclidean distance. Output: the minimum cost of a traveling salesman tour for this instance, rounded down to the nearest integer. And the pseudo-code is: Let A = 2-D array, indexed by subsets of {1, 2, ,3, ..., n} that contains 1 and destinations j belongs to {1, 2, 3,...n} 1. Base case: 2. if S = {0}, then A[S, 1] = 0; 3. else, A[S, 1] = Infinity. 4.for m = 2, 3,

partition of a list using dynamic programming

自作多情 提交于 2019-12-04 09:55:21
I have posted a bit here related to a project I have been trying to work on and I keep hitting design problems and have to design from scratch. So I'm wondering if I can post what I'm trying to do and someone can help me understand how I can get the result I want. BackGround: I'm new to programming and trying to learn. So I took a project that interested me which involves basically taking list and breaking down each number using only numbers from the list. I know I could easily brute force this(which I did) but I wanted to also learn Hbase, Hadoop, and parallel processing so I wanted do it in

Surface reconstruction from 2 planar contours [closed]

时光毁灭记忆、已成空白 提交于 2019-12-04 07:46:25
There is a class of algorithms for triangulation between two planar contours. These algorithms try to make a "good triangulation" to fill a space between these contours: One of them ( Optimal surface reconstruction from planar contours ) is based on the dynamic programming technique and uses a cost function for determining which triangulation is acceptable according to the minimum cost. A minimal triangle area as a cost function makes a good result in most of cases ( Triangulation of Branching Contours using Area Minimization ), but, unfortunately, not in all of them. For example when you have

Algorithm for 'good' number

夙愿已清 提交于 2019-12-04 06:58:09
问题 A give number x is 'good' if the sum of any two consecutive digit of the number x are between k and 2k. I need to find an algorithm that for a given number k and a given number n, find how many 'good' n-digit numbers exist. I made an implementation for this in PHP, but the complexity is to big (i am searching for all those 'good' number and counting them, so the complexity is O(10^n)). <?php $n = 5; $k = 5; $min = $k*1; $max = $k*2; $counter = 0; for ($i = pow(10, $n-1); $i<pow(10,$n); $i++)

Using dynamic programming in Haskell? [Warning: ProjectEuler 31 solution inside]

倾然丶 夕夏残阳落幕 提交于 2019-12-04 06:39:41
In solving projecteuler.net's problem #31 [ SPOILERS AHEAD ] (counting the number of ways to make 2£ with the British coins), I wanted to use dynamic programming. I started with OCaml, and wrote the short and very efficient following programming: open Num let make_dyn_table amount coins = let t = Array.make_matrix (Array.length coins) (amount+1) (Int 1) in for i = 1 to (Array.length t) - 1 do for j = 0 to amount do if j < coins.(i) then t.(i).(j) <- t.(i-1).(j) else t.(i).(j) <- t.(i-1).(j) +/ t.(i).(j - coins.(i)) done done; t let _ = let t = make_dyn_table 200 [|1;2;5;10;20;50;100;200|] in