forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-number-of-operations-to-make-x-and-y-equal.cpp
62 lines (59 loc) · 1.72 KB
/
minimum-number-of-operations-to-make-x-and-y-equal.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
// Time: O(x)
// Space: O(x)
// memoization
class Solution {
public:
int minimumOperationsToMakeEqual(int x, int y) {
unordered_map<int, int> lookup;
const function<int (int)> memoization = [&](int x) {
if (y >= x) {
return y - x;
}
if (!lookup.count(x)) {
lookup[x] = x - y;
for (const auto& d : {5, 11}) {
lookup[x] = min(lookup[x], min(x % d, d - x % d) + memoization(x / d + (d - x % d < x % d ? 1 : 0)) + 1);
}
}
return lookup[x];
};
return memoization(x);
}
};
// Time: O(x)
// Space: O(x)
// bfs
class Solution2 {
public:
int minimumOperationsToMakeEqual(int x, int y) {
if (y >= x) {
return y - x;
}
const int upper_bound = x + (x - y);
unordered_set<int> lookup = {x};
vector<int> q = {x};
for (int d = 0; !empty(q); ++d) {
vector<int> new_q;
for (const auto& x: q) {
if (x == y) {
return d;
}
vector<int> candidates = {x + 1, x - 1};
for (const auto& d : {5, 11}) {
if (x % d == 0) {
candidates.emplace_back(x / d);
}
}
for (const auto& new_x : candidates) {
if (!(0 <= new_x && new_x <= upper_bound && !lookup.count(new_x))) {
continue;
}
lookup.emplace(new_x);
new_q.emplace_back(new_x);
}
}
q = move(new_q);
}
return -1;
}
};