# Minimize cost to travel using given travel plans

Given a sorted integer array traveldays[] represents the days of a year one must travel, and two arrays cost[] and span[] array of size 3 each where cost[i] denotes the cost to travel for continuous span[i] days, the task is to find the minimum cost required to travel every day in the list of traveldays[].

Examples:

Input: traveldays[] = {1, 2, 3, 4, 5, 6, 7}, cost[] = {2, 5, 4}, span[] = {1, 15, 30}
Output: 4
Explanation:

Day 1: 3 choices are there either take 1 day travel plan and pay cost 2 and again plan from day 2 for rest of the days.
Take 15 days travel plan and travel 15 days from day 1 with total cost 5, or
Take 30 days travel plan and travel 30 consecutive days with total cost 4.
Using 1st choice cost to travel day 1 = 2, again for rest of the days from 2, …, 7 we have 3 choices again so it can be assumed that will cost more in total.
If 2nd choice is taken on day 1 i.e., travel maximum 15 days consecutive with total cost 5.
Travel days will be day: 1, 2, 3, 4, …., 15. Since traveldays = [1, 2, 3, 4, 5, 6, 7] are within this 15 days range.
So total cost by taking 15 days travel plan = 5.
If 3rd choice is taken on day 1, then 30 maximum consecutive days can be travelled using this plan.
Since these 7 days are within this 30 consecutive days.  Total cost = 4.

From above 3 choices, taking travel plan of 30 days, total cost is minimum.

Input: traveldays[] = {1, 3, 6, 7, 8, 20}, cost[] = {2, 7, 15}, span[] = {2, 3, 5}
Output: 10

Naive Approach: The easiest way to solve the problem is to check for all the three possibilities each day and find the minimum cost to travel on all the mentioned days.

Time Complexity: O(3N)
Auxiliary Space: O(1)

Efficient Approach: In the problem, there are 3 choices given for each travel day, i.e., either take span[0] day travel plan, or take span[1] days travel plan or span[2] days travel plan. This problem can be solved optimally with the help of dynamic programming based on the following idea:

Consider each day as the ending of a span and find the span which will result in minimum cost till that day. Finally, the value at the maximum day of the given travel days will be the required minimum cost.

Say the values are stored in dp[]. The dp[] transition will be:
dp[i] = min (cost[0] + dp[i-span[0]], cost[1] + dp[i-span[1]], cost[2] + dp[i – span[2]])

Follow the steps mentioned below to solve the problem using the above idea.

• Create an array (say minimumcost[]) of size 366 as a total of 365 days are there in a year.
• Find the last day to travel (the last value of traveldays[]) from the given array.
• Now run a loop from i = 1 to last day:
• Check if ith day is present in traveldays[] array or not.
• If not present then minimumcost[i] = minimumcost[i – 1].
• Else use the above transition function to find the minimum cost.
• The value of minimumcost[last day] will be the answer.

Below is the implementation of the above approach.

## C++

 `// C++ code to implement the approach `` ` `#include ``using` `namespace` `std; `` ` `// Function to calculate minimum cost ``int` `MinimumTravelCost(vector<``int``>& travelDays, ``                      ``vector<``int``>& cost, ``                      ``vector<``int``>& span) ``{ ``    ``vector<``int``> minimumCost(366, 0); ``    ``int` `N = travelDays.size(); `` ` `    ``int` `maxDay = travelDays[N - 1]; `` ` `    ``// Loop to implement dynamic programming ``    ``// to find the minimum cost to travel ``    ``for` `(``int` `i = 1; i <= maxDay; i++) { `` ` `        ``// If current day is not present ``        ``if` `(find(travelDays.begin(), ``                 ``travelDays.end(), i) ``            ``== travelDays.end()) { `` ` `            ``minimumCost[i] = minimumCost[i - 1]; ``        ``} ``        ``else` `{ `` ` `            ``// Try all possibilities ``            ``minimumCost[i] = min( ``                ``{ cost[0] ``                      ``+ minimumCost[max(0, i - span[0])], ``                  ``cost[1] ``                      ``+ minimumCost[max(0, i - span[1])], ``                  ``cost[2] ``                      ``+ minimumCost[max(0, i - span[2])] }); ``        ``} ``    ``} `` ` `    ``// Return the answer ``    ``return` `minimumCost[maxDay]; ``} `` ` `// Driver code ``int` `main() ``{ ``    ``vector<``int``> travelDays = { 1, 3, 4, 5, 6, 7 }; ``    ``vector<``int``> cost = { 2, 5, 4 }; ``    ``vector<``int``> span = { 1, 15, 30 }; `` ` `    ``// Function call ``    ``cout << MinimumTravelCost(travelDays, cost, span); ``    ``return` `0; ``}`

## Java

 `/*package whatever // do not write package name here */``// Contributed by akashish__ ``import` `java.io.*; `` ` `class` `GFG { ``    ``static` `int` `find(``int``[] arr, ``int` `num) ``    ``{ ``        ``int` `ans = -``1``; ``        ``for` `(``int` `i = ``0``; i < arr.length; i++) { ``            ``if` `(arr[i] == num) { ``                ``ans = ``1``; ``                ``break``; ``            ``} ``        ``} ``        ``return` `ans; ``    ``} `` ` `    ``public` `static` `int` `min(``int` `a, ``int` `b, ``int` `c) ``    ``{ ``        ``return` `Math.min(Math.min(a, b), c); ``    ``} `` ` `    ``static` `int` `MinimumTravelCost(``int``[] travelDays, ``                                 ``int``[] cost, ``                                 ``int``[] span) ``    ``{ ``        ``int``[] minimumCost = ``new` `int``[``370``]; ``        ``int` `N = travelDays.length; `` ` `        ``int` `maxDay = travelDays[N - ``1``]; `` ` `        ``// Loop to implement dynamic programming ``        ``// to find the minimum cost to travel ``        ``for` `(``int` `i = ``1``; i <= maxDay; i++) { `` ` `            ``// If current day is not present ``            ``if` `(find(travelDays, i) ``                ``== -``1``) { `` ` `                ``minimumCost[i] = minimumCost[i - ``1``]; ``            ``} ``            ``else` `{ `` ` `                ``// Try all possibilities ``                ``minimumCost[i] = min( ``                    ``(cost[``0``] ``                     ``+ minimumCost[(Math.max(``0``, i - span[``0``]))]), ``                    ``(cost[``1``] ``                     ``+ minimumCost[(Math.max(``0``, i - span[``1``]))]), ``                    ``(cost[``2``] ``                     ``+ minimumCost[(Math.max(``0``, i - span[``2``]))])); ``            ``} ``        ``} `` ` `        ``// Return the answer ``        ``return` `minimumCost[maxDay]; ``    ``} `` ` `    ``public` `static` `void` `main(String[] args) ``    ``{ `` ` `        ``int``[] travelDays = { ``1``, ``3``, ``4``, ``5``, ``6``, ``7` `}; ``        ``int``[] cost = { ``2``, ``5``, ``4` `}; ``        ``int``[] span = { ``1``, ``15``, ``30` `}; `` ` `        ``// Function call ``        ``int` `ans = MinimumTravelCost(travelDays, cost, span); ``        ``System.out.println(ans); ``    ``} ``} `` ` `// This code is contributed by akashish_.`

## Python3

 `# Python3 code to implement the approach `` ` `# Function to calculate minimum cost ``def` `MinimumTravelCost (travelDays, cost, span): ``    ``minimumCost ``=` `[``0``] ``*` `366``    ``N ``=` `len``(travelDays); `` ` `    ``maxDay ``=` `travelDays[N ``-` `1``]; `` ` `    ``# Loop to implement dynamic programming ``    ``# to find the minimum cost to travel ``    ``for` `i ``in` `range``(``1``, maxDay ``+` `1``): `` ` `        ``# If current day is not present ``        ``if` `(i ``not` `in` `travelDays): ``            ``minimumCost[i] ``=` `minimumCost[i ``-` `1``]; ``        ``else``: `` ` `            ``# Try all possibilities ``            ``minimumCost[i] ``=` `min``(cost[``0``] ``+` `minimumCost[``max``(``0``, i ``-` `span[``0``])], ``                ``cost[``1``] ``                ``+` `minimumCost[``max``(``0``, i ``-` `span[``1``])], ``                ``cost[``2``] ``                ``+` `minimumCost[``max``(``0``, i ``-` `span[``2``])]); `` ` ` ` `    ``# Return the answer ``    ``return` `minimumCost[maxDay]; `` ` ` ` `# Driver code ``travelDays ``=` `[``1``, ``3``, ``4``, ``5``, ``6``, ``7``]; ``cost ``=` `[``2``, ``5``, ``4``]; ``span ``=` `[``1``, ``15``, ``30``]; `` ` `# Function call ``print``(MinimumTravelCost(travelDays, cost, span)); `` ` `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# code to implement the approach ``using` `System; `` ` `public` `class` `GFG { `` ` `    ``static` `int` `find(``int``[] arr, ``int` `num) ``    ``{ ``        ``int` `ans = -1; ``        ``for` `(``int` `i = 0; i < arr.Length; i++) { ``            ``if` `(arr[i] == num) { ``                ``ans = 1; ``                ``break``; ``            ``} ``        ``} ``        ``return` `ans; ``    ``} `` ` `    ``public` `static` `int` `min(``int` `a, ``int` `b, ``int` `c) ``    ``{ ``        ``return` `Math.Min(Math.Min(a, b), c); ``    ``} `` ` `    ``// Function to calculate minimum cost ``    ``static` `int` `MinimumTravelCost(``int``[] travelDays, ``                                 ``int``[] cost, ``int``[] span) ``    ``{ ``        ``int``[] minimumCost = ``new` `int``[370]; ``        ``int` `N = travelDays.Length; `` ` `        ``int` `maxDay = travelDays[N - 1]; `` ` `        ``// Loop to implement dynamic programming ``        ``// to find the minimum cost to travel ``        ``for` `(``int` `i = 1; i <= maxDay; i++) { `` ` `            ``// If current day is not present ``            ``if` `(find(travelDays, i) == -1) { `` ` `                ``minimumCost[i] = minimumCost[i - 1]; ``            ``} ``            ``else` `{ `` ` `                ``// Try all possibilities ``                ``minimumCost[i] = min( ``                    ``(cost[0] ``                     ``+ minimumCost[(Math.Max(0, ``                                             ``i - span[0]))]), ``                    ``(cost[1] ``                     ``+ minimumCost[(Math.Max(0, ``                                             ``i - span[1]))]), ``                    ``(cost[2] ``                     ``+ minimumCost[(Math.Max(0, ``                                             ``i - span[2]))])); ``            ``} ``        ``} `` ` `        ``// Return the answer ``        ``return` `minimumCost[maxDay]; ``    ``} `` ` `    ``static` `public` `void` `Main() ``    ``{ `` ` `        ``int``[] travelDays = { 1, 3, 4, 5, 6, 7 }; ``        ``int``[] cost = { 2, 5, 4 }; ``        ``int``[] span = { 1, 15, 30 }; `` ` `        ``// Function call ``        ``int` `ans = MinimumTravelCost(travelDays, cost, span); ``        ``Console.WriteLine(ans); ``    ``} ``} ``// This code is contributed by Ishan Khandelwal`

## Javascript

 ``

Output
`4`

Time Complexity: O(N)
Auxiliary Space: O(1)

Previous
Next