int[][] memo = null; // 备忘录
int r(int idx, int[] days, int[] costs, int deadLine)
{
int a, b, c, min;
if (idx == days.length)
return 0;
// 有效期超过最后一天
if (deadLine >= days[days.length-1])
return 0;
// 如果天数包含在假期内,就不需要再花钱
while (deadLine >= days[idx])
idx++;
// 如果计算过
if (memo[idx][deadLine] != 0)
return memo[idx][deadLine];
a = r(idx, days, costs, days[idx]) + costs[0];
b = r(idx, days, costs, days[idx]+6) + costs[1];
c = r(idx, days, costs, days[idx]+29) + costs[2];
min = Math.min(Math.min(a, b), c);
memo[idx][deadLine] = min;
return memo[idx][deadLine];
}
public int mincostTickets(int[] days, int[] costs)
{
// 初始化备忘录
memo = new int[days.length][366];
return r(0, days, costs, 0);
}