-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path241.cpp
More file actions
48 lines (40 loc) · 1.35 KB
/
241.cpp
File metadata and controls
48 lines (40 loc) · 1.35 KB
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
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
unordered_map<string, vector<int>> memory;
public:
vector<int> diffWaysToCompute(string input) {
if(memory.find(input) != memory.end())
return memory[input];
vector<int> result;
for(int i = 0; i < input.size(); i++)
{
if(input[i] == '*' || input[i] == '+' || input[i] == '-')
{
vector<int> left = diffWaysToCompute(input.substr(0, i));
vector<int> right = diffWaysToCompute(input.substr(i+1));
for(int l : left)
{
for(int r : right)
{
switch(input[i])
{
case '*': result.emplace_back(l*r);
break;
case '+': result.emplace_back(l+r);
break;
case '-': result.emplace_back(l-r);
break;
}
}
}
}
}
if(result.empty())
result.emplace_back(stoi(input));
memory.insert(make_pair(input, result));
return result;
}
};