dynamic-programming

How to Determine Cheapest Commute Ticket Combination

自闭症网瘾萝莉.ら 提交于 2019-12-06 07:01:32
问题 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

What can be space efficient algorithm for Single row of skyscraper puzzle

倾然丶 夕夏残阳落幕 提交于 2019-12-06 04:29:03
问题 I am trying to solve a problem which is a single row variant of skyscraper puzzle. The problem statement is: Consider a single row of a skyscraper puzzle of size nxn. If we know how many buildings can be seen from the left, and from the right, of the row, how many different ways are there of populating that row with buildings of heights 1..n? (1<=n<=5000) I am solving it with bottom up dynamic programming. Recurrence relation is following: f(n,left,right)=(n-2)*f(n-1,left,right) + f(n-1,left

Surface reconstruction from 2 planar contours [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-06 03:36:12
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . 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

dynamic programming : traversal of cities

醉酒当歌 提交于 2019-12-06 03:13:04
I came across this question: There are two persons. There is an ordered sequence of n cities, and the distances between every pair of cities is given. You must partition the cities into two subsequences (not necessarily contiguous) such that person A visits all cities in the first subsequence (in order), person B visits all cities in the second subsequence (in order), and such that the sum of the total distances travelled by A and B is minimized. Assume that person A and person B start initially at the first city in their respective subsequences. I looked for its answer and the answer was: Let

Can a convolution function written in tail recursive form?

和自甴很熟 提交于 2019-12-06 03:10:13
I have a function that I want to write in tail recursive form. The function calculates the number of ways to get the sum of k by rolling an s sided die n times. I have seen the mathematical solution for this function on this answer . It is as follows: My reference recursive implementation in R is: sum_ways <- function(n_times, k_sum, s_side) { if (k_sum < n_times || k_sum > n_times * s_side) { return(0) } else if (n_times == 1) { return(1) } else { sigma_values <- sapply( 1:s_side, function(j) sum_ways(n_times - 1, k_sum - j, s_side) ) return(sum(sigma_values)) } } I have tried to re-write the

Dynamic programming approach to TSP in Java

大城市里の小女人 提交于 2019-12-06 03:04:34
I'm a beginner, and I'm trying to write a working travelling salesman problem using dynamic programming approach. This is the code for my compute function: public static int compute(int[] unvisitedSet, int dest) { if (unvisitedSet.length == 1) return distMtx[dest][unvisitedSet[0]]; int[] newSet = new int[unvisitedSet.length-1]; int distMin = Integer.MAX_VALUE; for (int i = 0; i < unvisitedSet.length; i++) { for (int j = 0; j < newSet.length; j++) { if (j < i) newSet[j] = unvisitedSet[j]; else newSet[j] = unvisitedSet[j+1]; } int distCur; if (distMtx[dest][unvisitedSet[i]] != -1) { distCur =

Maximum profit by buying and selling a share exactly k times

拥有回忆 提交于 2019-12-06 02:22:37
The cost of a bond on each day is given in array prices of length n , and I need to find the maximum profit that I can make by buying and selling in exactly k transactions (buying and selling, in that order. not in the same day. but I can sell and then buy in the same day). I tried (Python): prices = [3, 1, 10] n = len(prices) def aux(i, j): if j == n - 1 or i == 0: return 0 s = [(prices[j + t] - prices[j]) + aux(i - 1, j + t) for t in range(1, n - j)] return max(aux(i, j + 1), max(s)) if s else aux(i, j + 1) def max_profit(k): return aux(k, 0) But for the given array in the code, and with k=2

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

老子叫甜甜 提交于 2019-12-06 02:08:05
问题 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) +

Dynamic programming algorithm for facility locations

烈酒焚心 提交于 2019-12-05 22:30:32
There are n houses at locations a_1, a_2,..., a_n along a line. We want to set up porta potties along that same line so that every house is within distance R of at least one porta potty. These porta potties are restricted to the specified locations b_1, b_2,..., b_m. Let c_i be the cost of setting up a porta potty at location b_i. Find a dynamic programming algorithm that minimizes the total cost of setting up the porta potties. The algorithm should be able to detect if a solution does not exist. Assume that all a and b values are distinct. Inputs: A[1, 2,...n] holds the house locations B[1, 2

How to find a subarray with minimum k length and maximum sum?

只愿长相守 提交于 2019-12-05 20:53:22
The subarray contains both positive and negative numbers. You have to find a maximum sum subarray such that the length of the sub-array is greater than or equal to k. Here is my code in c++ using Kadane's algorithm. #include <iostream> using namespace std; int main(){ int n,k; cin >> n >> k; int array[n]; int sum = 0; int maxsum = 0; int beststarts[n]; for(int i = 0;i < n; i++){ cin >> array[i]; } for(int i = 0;i < k-1;i ++){ sum = sum+array[i]; beststarts[i] = 0; } for(int i = k-1;i < n; i++){ //best end search with min length; sum = sum+array[i]; int testsum = sum; if(i > 0){ beststarts[i] =