Skip to content

Commit

Permalink
fix the problem - out of array boundary, and unresonalble ticket pric…
Browse files Browse the repository at this point in the history
…e "[7,2,15]". close haoel#243
  • Loading branch information
haoel committed Mar 26, 2021
1 parent ff70235 commit 5183dad
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions algorithms/cpp/minimumCostForTickets/MinimumCostForTickets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ class Solution {
int mincostTickets(vector<int>& days, vector<int>& costs) {

// Dynamic Programming
vector<int> dp(days.size(), INT_MAX);
vector<int> dp(days.size()+1, INT_MAX);

// dp[i] is the minimal cost from Days[0] to Days[i]
dp[0] = costs[0];
dp[0] = 0;
dp[1] = min(costs[0], costs[1], costs[2]);

for (int i = 1; i< days.size(); i ++) {
for (int i = 2; i<= days.size(); i ++) {

// the currnet day need at least 1-day pass cost
int OneDayPass = dp[i-1] + costs[0];
// the currnet day need at least min(1-day, 7days, 30days) from previous.
int m = dp[i-1] + min(costs[0], costs[1], costs[2]);

// Seprating the array to two parts.
// days[0] -> days[j] -> day[i]
Expand All @@ -79,21 +80,23 @@ class Solution {
//
// Traking the minimal costs, then can have dp[i] minimal cost

int SevenDayPass = INT_MAX, ThrityDayPass = INT_MAX;
for (int j=i-1; j>=0; j--){
if (days[i] - days[j] < 7 ) {
SevenDayPass = dp[j-1] + costs[1];
} else if (days[i] - days[j] < 30 ) {
ThrityDayPass = dp[j-1] + costs[2];
int SevenDays = INT_MAX, ThrityDays = INT_MAX;
for (int j=i-1; j>0; j--){
int gaps = days[i-1] - days[j-1];
if ( gaps < 7 ) {
// can use 7-days or 30-days ticket
SevenDays = dp[j-1] + min(costs[1], costs[2]);
} else if (gaps < 30 ) {
//can use 30-days tickets
ThrityDays = dp[j-1] + costs[2];
} else {
break;
}
int m = min(OneDayPass, SevenDayPass, ThrityDayPass);
if ( dp[i] > m ) dp[i] = m;
m = min(m, SevenDays, ThrityDays);
}

if ( dp[i] > m ) dp[i] = m;
}

return dp[dp.size()-1];
return dp[dp.size()-1];
}
};

0 comments on commit 5183dad

Please sign in to comment.