Skip to content

Commit 3ca0b66

Browse files
committed
feat: add solutions to lc problem: No.0983
No.0983.Minimum Cost For Tickets
1 parent d066649 commit 3ca0b66

File tree

6 files changed

+422
-2
lines changed

6 files changed

+422
-2
lines changed

solution/0900-0999/0983.Minimum Cost For Tickets/README.md

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,170 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:记忆化搜索 + 二分查找**
67+
68+
定义 `dfs(i)` 表示从第 `i` 次出行开始的最低消费。答案为 `dfs(0)`
69+
70+
采用记忆化搜索的方法,记录已经计算过的结果,避免重复计算。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `days` 的长度。
73+
6674
<!-- tabs:start -->
6775

6876
### **Python3**
6977

7078
<!-- 这里可写当前语言的特殊实现逻辑 -->
7179

7280
```python
73-
81+
class Solution:
82+
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
83+
@cache
84+
def dfs(i):
85+
if i >= len(days):
86+
return 0
87+
res = inf
88+
for c, d in zip(costs, [1, 7, 30]):
89+
j = bisect_left(days, days[i] + d)
90+
res = min(res, c + dfs(j))
91+
return res
92+
93+
return dfs(0)
7494
```
7595

7696
### **Java**
7797

7898
<!-- 这里可写当前语言的特殊实现逻辑 -->
7999

80100
```java
101+
class Solution {
102+
private static final int[] T = new int[]{1, 7, 30};
103+
private int[] costs;
104+
private int[] days;
105+
private int[] f;
106+
private int n;
107+
108+
public int mincostTickets(int[] days, int[] costs) {
109+
n = days.length;
110+
f = new int[n];
111+
this.costs = costs;
112+
this.days = days;
113+
Arrays.fill(f, -1);
114+
return dfs(0);
115+
}
116+
117+
private int dfs(int i) {
118+
if (i >= n) {
119+
return 0;
120+
}
121+
if (f[i] != -1) {
122+
return f[i];
123+
}
124+
int res = Integer.MAX_VALUE;
125+
126+
for (int k = 0; k < 3; ++k) {
127+
int j = lowerBound(days, days[i] + T[k]);
128+
res = Math.min(res, costs[k] + dfs(j));
129+
}
130+
f[i] = res;
131+
return res;
132+
}
133+
134+
private int lowerBound(int[] days, int x) {
135+
int left = 0, right = days.length;
136+
while (left < right) {
137+
int mid = (left + right) >> 1;
138+
if (days[mid] >= x) {
139+
right = mid;
140+
} else {
141+
left = mid + 1;
142+
}
143+
}
144+
return left;
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
vector<int> t = {1, 7, 30};
155+
vector<int> days;
156+
vector<int> costs;
157+
vector<int> f;
158+
int n;
159+
160+
int mincostTickets(vector<int>& days, vector<int>& costs) {
161+
n = days.size();
162+
this->days = days;
163+
this->costs = costs;
164+
f.assign(n, -1);
165+
return dfs(0);
166+
}
167+
168+
int dfs(int i) {
169+
if (i >= n) return 0;
170+
if (f[i] != -1) return f[i];
171+
int res = INT_MAX;
172+
for (int k = 0; k < 3; ++k) {
173+
int j = lower_bound(days.begin(), days.end(), days[i] + t[k]) - days.begin();
174+
res = min(res, costs[k] + dfs(j));
175+
}
176+
f[i] = res;
177+
return res;
178+
}
179+
};
180+
```
81181
182+
### **Go**
183+
184+
```go
185+
func mincostTickets(days []int, costs []int) int {
186+
t := []int{1, 7, 30}
187+
n := len(days)
188+
f := make([]int, n)
189+
for i := range f {
190+
f[i] = -1
191+
}
192+
var dfs func(i int) int
193+
dfs = func(i int) int {
194+
if i >= n {
195+
return 0
196+
}
197+
if f[i] != -1 {
198+
return f[i]
199+
}
200+
res := 0x3f3f3f3f
201+
for k, c := range costs {
202+
j := lowerBound(days, days[i]+t[k])
203+
res = min(res, c+dfs(j))
204+
}
205+
f[i] = res
206+
return res
207+
}
208+
return dfs(0)
209+
}
210+
211+
func lowerBound(arr []int, x int) int {
212+
left, right := 0, len(arr)
213+
for left < right {
214+
mid := (left + right) >> 1
215+
if arr[mid] >= x {
216+
right = mid
217+
} else {
218+
left = mid + 1
219+
}
220+
}
221+
return left
222+
}
223+
224+
func min(a, b int) int {
225+
if a < b {
226+
return a
227+
}
228+
return b
229+
}
82230
```
83231

84232
### **TypeScript**

solution/0900-0999/0983.Minimum Cost For Tickets/README_EN.md

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,153 @@ In total, you spent $17 and covered all the days of your travel.
6464
### **Python3**
6565

6666
```python
67-
67+
class Solution:
68+
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
69+
@cache
70+
def dfs(i):
71+
if i >= len(days):
72+
return 0
73+
res = inf
74+
for c, d in zip(costs, [1, 7, 30]):
75+
j = bisect_left(days, days[i] + d)
76+
res = min(res, c + dfs(j))
77+
return res
78+
79+
return dfs(0)
6880
```
6981

7082
### **Java**
7183

7284
```java
85+
class Solution {
86+
private static final int[] T = new int[]{1, 7, 30};
87+
private int[] costs;
88+
private int[] days;
89+
private int[] f;
90+
private int n;
91+
92+
public int mincostTickets(int[] days, int[] costs) {
93+
n = days.length;
94+
f = new int[n];
95+
this.costs = costs;
96+
this.days = days;
97+
Arrays.fill(f, -1);
98+
return dfs(0);
99+
}
100+
101+
private int dfs(int i) {
102+
if (i >= n) {
103+
return 0;
104+
}
105+
if (f[i] != -1) {
106+
return f[i];
107+
}
108+
int res = Integer.MAX_VALUE;
109+
110+
for (int k = 0; k < 3; ++k) {
111+
int j = lowerBound(days, days[i] + T[k]);
112+
res = Math.min(res, costs[k] + dfs(j));
113+
}
114+
f[i] = res;
115+
return res;
116+
}
117+
118+
private int lowerBound(int[] days, int x) {
119+
int left = 0, right = days.length;
120+
while (left < right) {
121+
int mid = (left + right) >> 1;
122+
if (days[mid] >= x) {
123+
right = mid;
124+
} else {
125+
left = mid + 1;
126+
}
127+
}
128+
return left;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
vector<int> t = {1, 7, 30};
139+
vector<int> days;
140+
vector<int> costs;
141+
vector<int> f;
142+
int n;
143+
144+
int mincostTickets(vector<int>& days, vector<int>& costs) {
145+
n = days.size();
146+
this->days = days;
147+
this->costs = costs;
148+
f.assign(n, -1);
149+
return dfs(0);
150+
}
73151

152+
int dfs(int i) {
153+
if (i >= n) return 0;
154+
if (f[i] != -1) return f[i];
155+
int res = INT_MAX;
156+
for (int k = 0; k < 3; ++k) {
157+
int j = lower_bound(days.begin(), days.end(), days[i] + t[k]) - days.begin();
158+
res = min(res, costs[k] + dfs(j));
159+
}
160+
f[i] = res;
161+
return res;
162+
}
163+
};
164+
```
165+
166+
### **Go**
167+
168+
```go
169+
func mincostTickets(days []int, costs []int) int {
170+
t := []int{1, 7, 30}
171+
n := len(days)
172+
f := make([]int, n)
173+
for i := range f {
174+
f[i] = -1
175+
}
176+
var dfs func(i int) int
177+
dfs = func(i int) int {
178+
if i >= n {
179+
return 0
180+
}
181+
if f[i] != -1 {
182+
return f[i]
183+
}
184+
res := 0x3f3f3f3f
185+
for k, c := range costs {
186+
j := lowerBound(days, days[i]+t[k])
187+
res = min(res, c+dfs(j))
188+
}
189+
f[i] = res
190+
return res
191+
}
192+
return dfs(0)
193+
}
194+
195+
func lowerBound(arr []int, x int) int {
196+
left, right := 0, len(arr)
197+
for left < right {
198+
mid := (left + right) >> 1
199+
if arr[mid] >= x {
200+
right = mid
201+
} else {
202+
left = mid + 1
203+
}
204+
}
205+
return left
206+
}
207+
208+
func min(a, b int) int {
209+
if a < b {
210+
return a
211+
}
212+
return b
213+
}
74214
```
75215

76216
### **TypeScript**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> t = {1, 7, 30};
4+
vector<int> days;
5+
vector<int> costs;
6+
vector<int> f;
7+
int n;
8+
9+
int mincostTickets(vector<int>& days, vector<int>& costs) {
10+
n = days.size();
11+
this->days = days;
12+
this->costs = costs;
13+
f.assign(n, -1);
14+
return dfs(0);
15+
}
16+
17+
int dfs(int i) {
18+
if (i >= n) return 0;
19+
if (f[i] != -1) return f[i];
20+
int res = INT_MAX;
21+
for (int k = 0; k < 3; ++k) {
22+
int j = lower_bound(days.begin(), days.end(), days[i] + t[k]) - days.begin();
23+
res = min(res, costs[k] + dfs(j));
24+
}
25+
f[i] = res;
26+
return res;
27+
}
28+
};

0 commit comments

Comments
 (0)