Skip to content

Commit cb7186b

Browse files
committedMar 28, 2023
added problem 983
1 parent 5ec62c5 commit cb7186b

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
 
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package problem0983
2+
3+
/*
4+
You have planned some train traveling one year in advance.
5+
The days of the year in which you will travel are given as an integer array days.
6+
Each day is an integer from 1 to 365.
7+
Train tickets are sold in three different ways:
8+
a 1-day pass is sold for costs[0] dollars,
9+
a 7-day pass is sold for costs[1] dollars, and
10+
a 30-day pass is sold for costs[2] dollars.
11+
The passes allow that many days of consecutive travel.
12+
For example, if we get a 7-day pass on day 2, then we can travel for 7 days: 2, 3, 4, 5, 6, 7, and 8.
13+
Return the minimum number of dollars you need to travel every day in the given list of days.
14+
*/
15+
16+
func mincostTickets(days []int, costs []int) int {
17+
// cache stores the costs for the last 30 days of travel
18+
var cache [30]int
19+
// travel[i] tells us if we travel on the ith day
20+
var travel = make(map[int]struct{}, len(days))
21+
var l = len(days) - 1
22+
for _, d := range days {
23+
travel[d] = struct{}{}
24+
}
25+
26+
// Loop over all calendar days (not the given days)
27+
for i := 1; i <= 365; i++ {
28+
if _, ok := travel[i]; !ok {
29+
// If we don't travel this day, take the last day cost
30+
cache[i%30] = cache[(i-1)%30]
31+
} else {
32+
cache[i%30] = min(
33+
cache[(i-1)%30]+costs[0], // 1 day ticket
34+
cache[max(0, i-7)%30]+costs[1], // 7 day ticket
35+
cache[max(0, i-30)%30]+costs[2], // 30 day ticket
36+
)
37+
38+
}
39+
}
40+
41+
return cache[days[l]%30]
42+
}
43+
44+
func min(nums ...int) int {
45+
min := nums[0]
46+
for _, num := range nums {
47+
if num < min {
48+
min = num
49+
}
50+
}
51+
return min
52+
}
53+
54+
func max(nums ...int) int {
55+
max := nums[0]
56+
for _, num := range nums {
57+
if num > max {
58+
max = num
59+
}
60+
}
61+
return max
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem0983
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Days []int
12+
Costs []int
13+
Expected int
14+
}
15+
16+
var Results = []Result{
17+
{[]int{1, 4, 6, 7, 8, 20}, []int{2, 7, 15}, 11},
18+
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31}, []int{2, 7, 15}, 17},
19+
{[]int{1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 24, 25, 27, 28, 29, 30, 31, 34, 37, 38, 39, 41, 43, 44, 45, 47, 48, 49, 54, 57, 60, 62, 63, 66, 69, 70, 72, 74, 76, 78, 80, 81, 82, 83, 84, 85, 88, 89, 91, 93, 94, 97, 99}, []int{9, 38, 134}, 423},
20+
}
21+
22+
func TestMinCostOfTickets(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
got := mincostTickets(res.Days, res.Costs)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Each problem is in it's own directory, with test files. There are helper package
305305
| 0977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array) | [My Solution](./problems/problem0977) ||
306306
| 0980 | [Unique Paths III](https://leetcode.com/problems/unique-paths-iii) | [My Solution](./problems/problem0980) ||
307307
| 0981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store) | [My Solution](./problems/problem0981) ||
308+
| 0983 | [Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets) | [My Solution](./problems/problem0983) ||
308309
| 0985 | [Sum of Even Numbers After Queries](https://leetcode.com/problems/sum-of-even-numbers-after-queries) | [My Solution](./problems/problem0985) ||
309310
| 0987 | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree) | [My Solution](./problems/problem0987) ||
310311
| 0989 | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer) | [My Solution](./problems/problem0989) ||

0 commit comments

Comments
 (0)
Please sign in to comment.