forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
53 lines (38 loc) · 1.17 KB
/
main3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Source : https://leetcode.com/problems/minimum-cost-for-tickets/
/// Author : liuyubobobo
/// Time : 2019-01-28
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/// Memory Search
/// No need have 365 states, n states would be enough :-)
///
/// Time Complexity: O(n)
/// Space Complexity: O(len(days))
class Solution {
public:
int mincostTickets(vector<int>& days, vector<int>& costs) {
int n = days.size();
vector<int> dp(n, -1);
return dfs(days, 0, n, costs, dp);
}
private:
int dfs(const vector<int>& days, int index, int n, const vector<int>& costs, vector<int>& dp){
if(index >= n) return 0;
if(dp[index] != -1) return dp[index];
int res = costs[0] + dfs(days, index + 1, n, costs, dp);
int i1 = 0;
for(; i1 < n; i1 ++)
if(days[i1] >= days[index] + 7) break;
res = min(res, costs[1] + dfs(days, i1, n, costs, dp));
int i2 = 0;
for(; i2 < n; i2 ++)
if(days[i2] >= days[index] + 30) break;
res = min(res, costs[2] + dfs(days, i2, n, costs, dp));
return dp[index] = res;
}
};
int main() {
return 0;
}