forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest-dessert-cost.cpp
140 lines (131 loc) · 4.62 KB
/
closest-dessert-cost.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Time: O(m * max(max_base, target + max_topping / 2)) ~= O(m * t)
// Space: O(max(max_base, target + max_topping / 2)) ~= O(t)
class Solution {
public:
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
static const int MAX_COUNT = 2;
const auto max_base = *max_element(cbegin(baseCosts), cend(baseCosts));
const auto max_topping = *max_element(cbegin(toppingCosts), cend(toppingCosts));
vector<bool> dp(max(max_base, target + max_topping / 2) + 1);
for (const auto& b : baseCosts) {
dp[b] = true;
}
for (const auto& t : toppingCosts) {
for (int count = 0; count < MAX_COUNT; ++count) {
for (int i = size(dp) - 1 - t; i >= 1; --i) {
if (dp[i]) {
dp[i + t] = true;
}
}
}
}
int result = numeric_limits<int>::max();
for (int i = 1; i <= size(dp) - 1; ++i) {
if (!dp[i]) {
continue;
}
if (abs(i - target) < abs(result - target)) {
result = i;
}
if (i >= target) {
break;
}
}
return result;
}
};
// Time: O(n * 3^m)
// Space: O(m * t)
class Solution2 {
public:
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
unordered_map<int, unordered_set<int>> lookup;
int result = numeric_limits<int>::max();
for (const auto& b : baseCosts) {
backtracking(toppingCosts, 0, b, target, &lookup, &result);
}
return result;
}
private:
void backtracking(const vector<int>& toppingCosts,
int i, int cost, int target,
unordered_map<int, unordered_set<int>> *lookup,
int *result) {
static const int max_top = 2;
if (lookup->count(i) && (*lookup)[i].count(cost)) {
return;
}
(*lookup)[i].emplace(cost);
if (cost >= target || i == size(toppingCosts)) {
if (pair(abs(cost - target), cost) < pair(abs(*result - target), *result)) {
*result = cost;
}
return;
}
for (int j = 0; j <= max_top; ++j) {
backtracking(toppingCosts, i + 1, cost + j * toppingCosts[i], target, lookup, result);
}
}
};
// Time: O(3^m*log(3^m)) + O(n*log(3^m)) = O(m*(3^m + n))
// Space: O(3^m)
class Solution3 {
public:
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
static const int MAX_COUNT = 2;
unordered_set<int> combs_set = {0};
for (const auto& t : toppingCosts) {
unordered_set<int> new_combs_set;
for (const auto& c : combs_set) {
for (int i = 0; i <= MAX_COUNT; ++i) {
new_combs_set.emplace(c + i * t);
}
}
combs_set = move(new_combs_set);
}
vector<int> combs(cbegin(combs_set), cend(combs_set));
sort(begin(combs), end(combs));
int result = numeric_limits<int>::max();
for (const auto& b : baseCosts) {
const auto& cit = lower_bound(cbegin(combs), cend(combs), target - b);
if (cit != cend(combs)) {
if (pair(abs(b + *cit - target), b + *cit) < pair(abs(result - target), result)) {
result = b + *cit;
}
}
if (cit != cbegin(combs)) {
if (pair(abs(b + *prev(cit) - target), b + *prev(cit)) < pair(abs(result - target), result)) {
result = b + *prev(cit);
}
}
}
return result;
}
};
// Time: O(n * 3^m)
// Space: O(3^m)
class Solution4 {
public:
int closestCost(vector<int>& baseCosts, vector<int>& toppingCosts, int target) {
static const int MAX_COUNT = 2;
unordered_set<int> combs_set = {0};
for (const auto& t : toppingCosts) {
unordered_set<int> new_combs_set;
for (const auto& c : combs_set) {
for (int i = 0; i <= MAX_COUNT; ++i) {
new_combs_set.emplace(c + i * t);
}
}
combs_set = move(new_combs_set);
}
int result = numeric_limits<int>::max();
for (const auto& b : baseCosts) {
for (const auto& c : combs_set) {
if (pair(abs(b + c - target), b + c) < pair(abs(result - target), result)) {
result = b + c;
}
}
}
return result;
}
};