Skip to content

Commit 37060b6

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 13 MB (55.34%)
1 parent 44bbd3a commit 37060b6

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<p>You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer array <code>days</code>. Each day is an integer from <code>1</code> to <code>365</code>.</p>
2+
3+
<p>Train tickets are sold in <strong>three different ways</strong>:</p>
4+
5+
<ul>
6+
<li>a <strong>1-day</strong> pass is sold for <code>costs[0]</code> dollars,</li>
7+
<li>a <strong>7-day</strong> pass is sold for <code>costs[1]</code> dollars, and</li>
8+
<li>a <strong>30-day</strong> pass is sold for <code>costs[2]</code> dollars.</li>
9+
</ul>
10+
11+
<p>The passes allow that many days of consecutive travel.</p>
12+
13+
<ul>
14+
<li>For example, if we get a <strong>7-day</strong> pass on day <code>2</code>, then we can travel for <code>7</code> days: <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, and <code>8</code>.</li>
15+
</ul>
16+
17+
<p>Return <em>the minimum number of dollars you need to travel every day in the given list of days</em>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> days = [1,4,6,7,8,20], costs = [2,7,15]
24+
<strong>Output:</strong> 11
25+
<strong>Explanation:</strong> For example, here is one way to buy passes that lets you travel your travel plan:
26+
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
27+
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
28+
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
29+
In total, you spent $11 and covered all the days of your travel.
30+
</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
36+
<strong>Output:</strong> 17
37+
<strong>Explanation:</strong> For example, here is one way to buy passes that lets you travel your travel plan:
38+
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
39+
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
40+
In total, you spent $17 and covered all the days of your travel.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= days.length &lt;= 365</code></li>
48+
<li><code>1 &lt;= days[i] &lt;= 365</code></li>
49+
<li><code>days</code> is in strictly increasing order.</li>
50+
<li><code>costs.length == 3</code></li>
51+
<li><code>1 &lt;= costs[i] &lt;= 1000</code></li>
52+
</ul>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int mincostTickets(vector<int>& days, vector<int>& costs) {
4+
int lastDay = days[days.size() - 1];
5+
vector<int> dp(lastDay + 1, 0);
6+
7+
int i = 0;
8+
for (int day = 1; day <= lastDay; day++) {
9+
if (day < days[i]) {
10+
dp[day] = dp[day - 1];
11+
} else {
12+
i++;
13+
dp[day] = min({dp[day - 1] + costs[0],
14+
dp[max(0, day - 7)] + costs[1],
15+
dp[max(0, day - 30)] + costs[2]});
16+
}
17+
}
18+
19+
return dp[lastDay];
20+
}
21+
};

0 commit comments

Comments
 (0)