leetcode983

public class Solution
    {
        public int MincostTickets(int[] days, int[] costs)
        {
            int weeklyStart = 0;
            int monthlyStart = 0;
            int[] dp = new int[days.Length + 1];
            dp[0] = 0;

            for (int i = 1; i <= days.Length; i++)
            {
                // day pass
                dp[i] = dp[i - 1] + costs[0];

                // update best weekly pass start date
                while (days[i - 1] - days[weeklyStart] >= 7) weeklyStart++;

                // update best monthly pass start date
                while (days[i - 1] - days[monthlyStart] >= 30) monthlyStart++;

                // get minimum of all 3 above
                dp[i] = Math.Min(dp[i], Math.Min(dp[weeklyStart] + costs[1], dp[monthlyStart] + costs[2]));
            }

            return dp[days.Length];
        }
    }

猜你喜欢

转载自www.cnblogs.com/asenyang/p/10331351.html