zoukankan      html  css  js  c++  java
  • Leetcode983 Minimum Cost For Tickets

    Description

    Problem

    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.

    Sample

    Sample1

    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.

    Sample2

    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.

    Analysis

    不方便看出线性关系时一定要考虑全部的不变量,即使往外扩展有多种可能,不变的还是天数之间的差值,以此列出状态转移方程

    Code

    以旅游日入手

    考虑upper_bound

    dp(j)=min(dp(j-1)+costs[0],dp(j-7)+costs[1],dp(j-30)+costs[2])

    class Solution {
    public:
        int mincostTickets(vector<int>& days, vector<int>& costs) {
            int n=days.size();
            auto start=days.begin();
            for(int i=0;i<n;++i){
                int b=(int)(upper_bound(start,start+i,days[i]-7)-start);
                int c=(int)(upper_bound(start,start+i,days[i]-30)-start);
                dp[i+1]=min(dp[i]+costs[0],min(dp[b]+costs[1],dp[c]+costs[2]));
            }
            return dp[n];
        }
    private:
            int dp[366];
    };
    

    以365天入手

    dp(i)=dp(i-1)

    dp(i)=min{cost(j)+dp(i-j)},j∈{1,7,30}

    class Solution {
    public:
        int mincostTickets(vector<int>& days, vector<int>& costs) {
            int n=days.size();
            int end=days[n-1];
            int idx=0;
            int x,y;
            for(int i=days[0];i<=end;++i){
                if(i==days[idx]){
                    x=i-7>=0?dp[i-7]:0;
                    y=i-30>=0?dp[i-30]:0;
                    dp[i]=min(dp[i-1]+costs[0],min(x+costs[1],y+costs[2]));
                    idx++;
                }
                else dp[i]=dp[i-1];
            }
            return dp[end];
        }
    private:
            int dp[366];
    };
    
  • 相关阅读:
    Maven中使用描述文件切换环境配置
    整合MyBatis到Spring中实现Dao层自动装配
    使用MyBatis搭建项目时报 java.io.IOException: Could not find resource
    数据库CPU占用高排查
    JS 根据时区获取时间
    国外服务器 winserver2012 安装IIS后,安装urlrewrite模块总是自动停止应用程序池
    sql中char(9) char(10) char(13)
    通过 Microsoft.Ace.OLEDB 接口导入 EXCEL 到SQLSERVER
    SDL 当前连接查询脚本
    C# System.Drawing.Graphics 画图后,如何保存一个低质量的图片,一个占用空间较小的图片
  • 原文地址:https://www.cnblogs.com/chanceYu/p/12839666.html
Copyright © 2011-2022 走看看