Java&LeetCode 初入门——983. Minimum Cost For Tickets最小票价问题)

文内代码全部采用JAVA语言。

题目

In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.

Train tickets are sold in 3 different ways:

a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.

Return the minimum number of dollars you need to travel every day in the given list of days.
在一个受火车旅行欢迎的国家,您计划提前一年旅行一些火车。 您将要旅行的一年中的几天作为阵列日。 每天是1到365之间的整数。

火车票以3种不同的方式出售:

成本为[0]美元,售出1天通行证;
售价为7天的通票售价为1美元;
30天的通票售价为2美元。
通行证允许连续多日旅行。 例如,如果我们在第2天获得7天通行证,那么我们可以旅行7天:第2,3,4,5,6,7和8天。

在给定的天数列表中,返回您需要旅行的最少数量的美元。

测试用例

Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation: 
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation: 
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.

个人解法

比较简单的动态规划问题。
三种票:日票,周票,和月票。出行日期存在days数组中。
假设第i天最少花费 d[i] 的钱; 第i+1天花的钱就是d[i+1]=d[i]+cost[0];但是如果在i+1天买周票,那么包括i+1在内的之前的七天都不要付费了,这些天的钱都白花了,所以买周票的话,i+1天最少花的d[i+1]=d[i-6]+cost[1];买月票同理, d[i+1]=d[i-29]+cost[2]; 三种买票方式里花的最少的就更新为d[i+1];
当然,如果i+1还不到6或者29的话,就是d[i+1]=0+costs;
注意,要花钱的日子肯定是days中存储的日子,如果这一天不需要出行,自然d[i+1]=d[i]
代码如下:

class Solution {
    public int mincostTickets(int[] days, int[] costs) {
        int[] ans=new int [366];
        int index=0;
        for(int i=1;i<=365;i++){
            if(i!=days[index]){
                ans[i]=ans[i-1];
            }else{
                int pay1=ans[i-1]+costs[0];
                int pay7=i>=7?(ans[i-7]+costs[1]):costs[1];
                int pay30=i>=30?(ans[i-30]+costs[2]):costs[2];
                ans[i]=Math.min(Math.min(pay1,pay7),pay30);
                index++;
                if(index>=days.length){
                    break;
                }
            }
        }
        return ans[days[days.length-1]];
    }
}

官方解法

方法1:Dynamic Programming (Day Variant)

和个人解法一致
对于每一天,如果您今天不必旅行,那么等待购买通行证更好。 如果您今天必须旅行,您最多有3种选择:您必须购买1天,7天或30天的通行证。

我们可以将这些选择表达为递归并使用动态编程。 假设dp(i)是从第i天到计划结束时完成旅行计划的费用。 那么,如果您今天必须旅行,您的费用是:

在这里插入图片描述

算法

class Solution {
    int[] costs;
    Integer[] memo;
    Set<Integer> dayset;

    public int mincostTickets(int[] days, int[] costs) {
        this.costs = costs;
        memo = new Integer[366];
        dayset = new HashSet();
        for (int d: days) dayset.add(d);

        return dp(1);
    }

    public int dp(int i) {
        if (i > 365)
            return 0;
        if (memo[i] != null)
            return memo[i];

        int ans;
        if (dayset.contains(i)) {
            ans = Math.min(dp(i+1) + costs[0],
                               dp(i+7) + costs[1]);
            ans = Math.min(ans, dp(i+30) + costs[2]);
        } else {
            ans = dp(i+1);
        }

        memo[i] = ans;
        return ans;
    }
}

方法2:Dynamic Programming (Window Variant)

与方法1一样,我们只需要在我们打算旅行的那一天购买旅行通行证。

现在,让dp(i)成为从日[i]到计划结束的旅行费用。 如果说,j1是最大的指数,如天[j1] <天[i] + 1,则j7是最大的指数,因此天[j7] <天[i] + 7,而j30是最大的指数,这样的天数 [j30] <天[i] + 30,然后我们有:
在这里插入图片描述

算法

class Solution {
    int[] days, costs;
    Integer[] memo;
    int[] durations = new int[]{1, 7, 30};

    public int mincostTickets(int[] days, int[] costs) {
        this.days = days;
        this.costs = costs;
        memo = new Integer[days.length];

        return dp(0);
    }

    public int dp(int i) {
        if (i >= days.length)
            return 0;
        if (memo[i] != null)
            return memo[i];

        int ans = Integer.MAX_VALUE;
        int j = i;
        for (int k = 0; k < 3; ++k) {
            while (j < days.length && days[j] < days[i] + durations[k])
                j++;
            ans = Math.min(ans, dp(j) + costs[k]);
        }

        memo[i] = ans;
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值