|
| 1 | +/* |
| 2 | +983. Minimum Cost For Tickets |
| 3 | +Medium |
| 4 | +
|
| 5 | +You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array days. Each day is an integer from 1 to 365. |
| 6 | +Train tickets are sold in three different ways: |
| 7 | +
|
| 8 | +a 1-day pass is sold for costs[0] dollars, |
| 9 | +a 7-day pass is sold for costs[1] dollars, and |
| 10 | +a 30-day pass is sold for costs[2] dollars. |
| 11 | +The passes allow that many days of consecutive travel. |
| 12 | +
|
| 13 | +For example, if we get a 7-day pass on day 2, then we can travel for 7 days: 2, 3, 4, 5, 6, 7, and 8. |
| 14 | +Return the minimum number of dollars you need to travel every day in the given list of days. |
| 15 | +
|
| 16 | +Example 1: |
| 17 | +
|
| 18 | +Input: days = [1,4,6,7,8,20], costs = [2,7,15] |
| 19 | +Output: 11 |
| 20 | +Explanation: For example, here is one way to buy passes that lets you travel your travel plan: |
| 21 | +On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1. |
| 22 | +On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9. |
| 23 | +On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20. |
| 24 | +In total, you spent $11 and covered all the days of your travel. |
| 25 | +
|
| 26 | +Example 2: |
| 27 | +Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15] |
| 28 | +Output: 17 |
| 29 | +Explanation: For example, here is one way to buy passes that lets you travel your travel plan: |
| 30 | +On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30. |
| 31 | +On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31. |
| 32 | +In total, you spent $17 and covered all the days of your travel. |
| 33 | + |
| 34 | +Constraints: |
| 35 | +
|
| 36 | +1 <= days.length <= 365 |
| 37 | +1 <= days[i] <= 365 |
| 38 | +days is in strictly increasing order. |
| 39 | +costs.length == 3 |
| 40 | +1 <= costs[i] <= 1000 |
| 41 | +*/ |
| 42 | + |
| 43 | + |
| 44 | +class Solution { |
| 45 | + static int[][] memo= new int[366][500]; |
| 46 | + static int[]memoB= new int[366]; |
| 47 | + public int mincostTickets(int[] days, int[] costs) { |
| 48 | + //Recursion :: TC =O(3^n) |
| 49 | + // SC : O(1)+O(n) |
| 50 | + //return solveRec(0,-1,days,costs); |
| 51 | + |
| 52 | + // Memoisation :: |
| 53 | + // TC : O(n*n) |
| 54 | + // SC: O(n*n) +O(n) |
| 55 | + // Arrays.stream(memo).forEach(a->Arrays.fill(a,-1)); |
| 56 | + // return solveMemo(0,0,days,costs); |
| 57 | + |
| 58 | + //Optimised:: Using Binary Search |
| 59 | + // TC : O(nlogn) |
| 60 | + // SC : O(n) |
| 61 | + Arrays.fill(memoB,-1); |
| 62 | + return solveMemoBS(0,days,costs); |
| 63 | + } |
| 64 | + public static int solveRec(int indx,int validity,int[] days,int[] cost){ |
| 65 | + //Base Case:: |
| 66 | + if(indx>=days.length) return 0; |
| 67 | + |
| 68 | + //choices based on validity:: |
| 69 | + if(validity<days[indx]){ |
| 70 | + int oneday = cost[0]+solveRec(indx+1,days[indx]+1-1,days,cost); |
| 71 | + int sevenday =cost[1] +solveRec(indx+1,days[indx]+7-1,days,cost); |
| 72 | + int thirtyday =cost[2]+solveRec(indx+1,days[indx]+30-1,days,cost); |
| 73 | + |
| 74 | + return Math.min(oneday,Math.min(sevenday,thirtyday)); |
| 75 | + }else{ |
| 76 | + return solveRec(indx+1,validity,days,cost); |
| 77 | + } |
| 78 | + } |
| 79 | + public static int solveMemo(int indx,int validity,int[] days,int[] cost){ |
| 80 | + //Base Case:: |
| 81 | + if(indx>=days.length) return 0; |
| 82 | + if(memo[indx][validity]!=-1){ |
| 83 | + return memo[indx][validity]; |
| 84 | + } |
| 85 | + //choices based on validity:: |
| 86 | + if(validity<days[indx]){ |
| 87 | + int oneday = cost[0]+solveMemo(indx+1,days[indx]+1-1,days,cost); |
| 88 | + int sevenday =cost[1] +solveMemo(indx+1,days[indx]+7-1,days,cost); |
| 89 | + int thirtyday =cost[2]+solveMemo(indx+1,days[indx]+30-1,days,cost); |
| 90 | + |
| 91 | + return memo[indx][validity]=Math.min(oneday,Math.min(sevenday,thirtyday)); |
| 92 | + }else{ |
| 93 | + return memo[indx][validity] =solveMemo(indx+1,validity,days,cost); |
| 94 | + } |
| 95 | + } |
| 96 | + public static int solveMemoBS(int indx,int[] days,int[] cost){ |
| 97 | + //Base Case:: |
| 98 | + if(indx>=days.length) return 0; |
| 99 | + if(memoB[indx]!=-1){ |
| 100 | + return memoB[indx]; |
| 101 | + } |
| 102 | + int day = binarySearch(indx,days[indx]+1,days); |
| 103 | + int oneday = cost[0]+solveMemoBS(day,days,cost); |
| 104 | + |
| 105 | + day =binarySearch(indx,days[indx]+7,days); |
| 106 | + int sevenday =cost[1] +solveMemoBS(day,days,cost); |
| 107 | + |
| 108 | + day =binarySearch(indx,days[indx]+30,days); |
| 109 | + int thirtyday =cost[2]+solveMemoBS(day,days,cost); |
| 110 | + |
| 111 | + return memoB[indx]=Math.min(oneday,Math.min(sevenday,thirtyday)); |
| 112 | + |
| 113 | + } |
| 114 | + //trying to find the first target index if not present first greatest value index to it:: |
| 115 | + public static int binarySearch(int low,int target,int days[]){ |
| 116 | + int high=days.length-1; |
| 117 | + while(low<=high){ |
| 118 | + int mi =low+(high-low)/2; |
| 119 | + if(days[mi]==target){ |
| 120 | + return mi; |
| 121 | + }else if(days[mi]>target){ |
| 122 | + high=mi-1; |
| 123 | + }else{ |
| 124 | + low=mi+1; |
| 125 | + } |
| 126 | + } |
| 127 | + return low; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments