forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimize-result-by-adding-parentheses-to-expression.cpp
106 lines (103 loc) · 3.88 KB
/
minimize-result-by-adding-parentheses-to-expression.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
// Time: O(n^2)
// Space: O(1)
// brute force
class Solution {
public:
string minimizeResult(string expression) {
const auto& stoi = [](const auto& s, int i, int j) {
if (i == j) {
return 1;
}
int result = 0;
for (int k = i; k < j; ++k) {
result = result * 10 + (s[k] - '0');
}
return result;
};
const auto& pos = expression.find('+');
int left = stoi(expression, 0, pos), right = stoi(expression, pos + 1, size(expression));
int min_val = numeric_limits<int>::max();
pair<int, int> best;
const int base2_init = pow(10, size(expression) - (pos + 1) - 1);
for (int i = 0, base1 = pow(10, pos); i < pos; ++i, base1 /= 10) {
for (int j = pos + 1, base2 = base2_init; j < size(expression); ++j, base2 /= 10) {
const int a = left / base1, b = left % base1;
const int c = right / base2, d = right % base2;
const auto& val = max(a, 1) * (b + c) * max(d, 1);
if (val < min_val) {
min_val = val;
best = {i, j};
}
}
}
string result;
for (int i = 0; i < size(expression); ++i) {
if (i == best.first) {
result.push_back('(');
}
result.push_back(expression[i]);
if (i == best.second) {
result.push_back(')');
}
}
return result;
}
};
// Time: O(n^2)
// Space: O(n)
// brute force
class Solution2 {
public:
string minimizeResult(string expression) {
const auto& pos = expression.find('+');
int left = stoi(expression.substr(0, pos)), right = stoi(expression.substr(pos + 1)); // Space: O(n)
int min_val = numeric_limits<int>::max();
pair<int, int> best;
const int base2_init = pow(10, size(expression) - (pos + 1) - 1);
for (int i = 0, base1 = pow(10, pos); i < pos; ++i, base1 /= 10) {
for (int j = pos + 1, base2 = base2_init; j < size(expression); ++j, base2 /= 10) {
const int a = left / base1, b = left % base1;
const int c = right / base2, d = right % base2;
const auto& val = max(a, 1) * (b + c) * max(d, 1);
if (val < min_val) {
min_val = val;
best = {i, j};
}
}
}
string result = expression.substr(0, best.first); // Space: O(n)
result += "(";
result += expression.substr(best.first, best.second + 1 - best.first); // Space: O(n)
result += ")";
result += expression.substr(best.second + 1); // Space: O(n)
return result;
}
};
// Time: O(n^3)
// Space: O(n)
// brute force
class Solution3 {
public:
string minimizeResult(string expression) {
const auto& pos = expression.find('+');
int min_val = numeric_limits<int>::max();
pair<int, int> best;
for (int i = 0; i < pos; ++i) {
for (int j = pos + 1; j < size(expression); ++j) {
const auto& val = (i != 0 ? stoi(expression.substr(0, i)) : 1) *
(stoi(expression.substr(i, pos - i)) + stoi(expression.substr(pos + 1, j + 1 - (pos + 1)))) *
(j + 1 != size(expression) ? stoi(expression.substr(j + 1)) : 1); // Space: O(n)
if (val < min_val) {
min_val = val;
best = {i, j};
}
}
}
string result = expression.substr(0, best.first); // Space: O(n)
result += "(";
result += expression.substr(best.first, best.second + 1 - best.first); // Space: O(n)
result += ")";
result += expression.substr(best.second + 1); // Space: O(n)
return result;
}
};