Skip to content

Commit 379440d

Browse files
committed
feat: Solution for August Challeng 25 - Minimum Cost For Tickets
1 parent adea77e commit 379440d

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*:
2+
# Minimum Cost For Tickets
3+
4+
https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3436/
5+
6+
---
7+
8+
### Problem Statement:
9+
10+
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`.
11+
12+
Train tickets are sold in 3 different ways:
13+
14+
+ a 1-day pass is sold for `costs[0]` dollars;
15+
+ a 7-day pass is sold for `costs[1]` dollars;
16+
+ a 30-day pass is sold for `costs[2]` dollars.
17+
18+
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.
19+
20+
Return the minimum number of dollars you need to travel every day in the given list of `days`.
21+
22+
### Example 1:
23+
24+
```
25+
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
26+
Output: 11
27+
Explanation:
28+
For example, here is one way to buy passes that lets you travel your travel plan:
29+
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
30+
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
31+
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
32+
In total you spent $11 and covered all the days of your travel.
33+
34+
```
35+
36+
### Example 2:
37+
38+
```
39+
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
40+
Output: 17
41+
Explanation:
42+
For example, here is one way to buy passes that lets you travel your travel plan:
43+
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
44+
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
45+
In total you spent $17 and covered all the days of your travel.
46+
47+
```
48+
49+
### Notes:
50+
+ 1 <= days.length <= 365
51+
+ 1 <= days[i] <= 365
52+
+ days is in strictly increasing order.
53+
+ costs.length == 3
54+
+ 1 <= costs[i] <= 1000
55+
56+
*/
57+
58+
59+
import UIKit
60+
61+
class Solution {
62+
63+
// 66 / 66 test cases passed.
64+
// Status: Accepted
65+
// Runtime: 104 ms
66+
// Memory Usage: 20.9 MB
67+
68+
func mincostTickets(_ days: [Int], _ costs: [Int]) -> Int {
69+
70+
guard days.count > 0, costs.count > 0 else {
71+
return 0
72+
}
73+
74+
var dp = Array(repeating: 0, count: days.last! + 1)
75+
76+
for i in 1..<dp.count {
77+
if days.contains(i) {
78+
79+
let option1 = dp[i - 1] + costs[0] // daily
80+
let option2 = dp[max(i - 7, 0)] + costs[1] // weekly
81+
let option3 = dp[max(i - 30, 0)] + costs[2] // monthly
82+
83+
dp[i] = min(option1, option2, option3)
84+
85+
} else {
86+
dp[i] = dp[i - 1]
87+
}
88+
89+
}
90+
91+
return dp.last!
92+
}
93+
94+
// Leetcode Solution
95+
func mincostTickets2(_ days: [Int], _ costs: [Int]) -> Int {
96+
var dp = Array(repeating:Int.max, count: days.count)
97+
dp[0] = costs.min()!
98+
let costs = zip([1, 7, 30], costs)
99+
for (i, day) in days.enumerated().dropFirst() {
100+
for (duration, cost) in costs {
101+
var j = i
102+
while j >= 0 && (day-days[j]) < duration {
103+
j -= 1
104+
}
105+
dp[i] = min(dp[i], (j >= 0 ? dp[j] : 0) + cost)
106+
}
107+
}
108+
109+
return dp.last ?? 0
110+
}
111+
}
112+
113+
let sol = Solution()
114+
sol.mincostTickets([1,2,3,4,5,6,7,8,9,10,30,31], [2,7,15]) // 17
115+
sol.mincostTickets2([1,2,3,4,5,6,7,8,9,10,30,31], [2,7,15]) // 17
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)