forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main5.cpp
50 lines (35 loc) · 1.25 KB
/
main5.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
/// Source : https://leetcode.com/problems/minimum-cost-for-tickets/
/// Author : liuyubobobo
/// Time : 2019-01-26
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/// Memory Search
/// No need have 365 states, n states would be enough :-)
/// Since days is in increasing order, we can use binary search to get next state more quickly :-)
///
/// 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 = upper_bound(days.begin(), days.end(), days[index] + 6) - days.begin();
res = min(res, costs[1] + dfs(days, i1, n, costs, dp));
int i2 = upper_bound(days.begin(), days.end(), days[index] + 29) - days.begin();
res = min(res, costs[2] + dfs(days, i2, n, costs, dp));
return dp[index] = res;
}
};
int main() {
return 0;
}