forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
minimum-number-of-days-to-eat-n-oranges.cpp
57 lines (54 loc) · 1.59 KB
/
minimum-number-of-days-to-eat-n-oranges.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
// Time: O((logn)^2)
// Space: O((logn)^2)
// complexity analysis: see https://leetcode.com/problems/minimum-number-of-days-to-eat-n-oranges/discuss/794847/Polylogarithmic-solution
class Solution {
public:
int minDays(int n) {
unordered_map<int, int> lookup;
return memoization(n, &lookup);
}
private:
int memoization(int i, unordered_map<int, int> *lookup) {
if (i <= 1) {
return i;
}
if (!lookup->count(i)) {
(*lookup)[i] = 1 + min(i % 2 + memoization(i / 2, lookup),
i % 3 + memoization(i / 3, lookup));
}
return (*lookup)[i];
}
};
// Time: O((logn)^2)
// Space: O((logn)^2)
class Solution2 {
public:
int minDays(int n) {
int result = 0;
vector<int> q = {n};
unordered_set<int> lookup = {n};
while (!q.empty()) {
vector<int> new_q;
for (const auto& i : q) {
if (i == 0) {
return result;
}
if (!lookup.count(i - 1)) {
lookup.emplace(i - 1);
new_q.emplace_back(i - 1);
}
if (i % 2 == 0 && !lookup.count(i / 2)) {
lookup.emplace(i / 2);
new_q.emplace_back(i / 2);
}
if (i % 3 == 0 && !lookup.count(i / 3)) {
lookup.emplace(i / 3);
new_q.emplace_back(i / 3);
}
}
++result;
q = move(new_q);
}
return result;
}
};