haoel / leetcode

LeetCode Problems' Solutions
17.58k stars 4.91k forks source link

out of range #243

Closed wanghuahui closed 3 years ago

wanghuahui commented 3 years ago

MinimumCostForTickets.cpp dp[j-1]有越界,做了如下修改

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];
    } else {
        break;
    } 
}
int m = min(OneDayPass, SevenDayPass, ThrityDayPass);
if ( dp[i] > m )  dp[i] = m;
haoel commented 3 years ago

Thanks for this finding, however, it's still wrong.

The original test cases added more unreasonable cases. such as:

[1,4,6,7,8,20]
[7,2,15]

can you impage that? the 7-days ticket is cheaper than 1-day ticket...

I will going to fix this problem soon!