-
Notifications
You must be signed in to change notification settings - Fork 8
/
DifferentWaysToAddParentheses.java
105 lines (90 loc) · 3.93 KB
/
DifferentWaysToAddParentheses.java
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
package divideandconquer;
// Source : https://leetcode.com/problems/different-ways-to-add-parentheses/
// Id : 241
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2019-05-24
// Topic : Divide & Conquer
// Level : Medium
// Other :
// Tips : "improvements" can make program run slower
// Result : 100% 100%
import java.util.*;
public class DifferentWaysToAddParentheses {
//32.84% 3 ms 34.61% 39.6 MB
public List<Integer> diffWaysToComputeSlow(String input) {
List<Integer> result = new LinkedList<>();
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') {
String part1 = input.substring(0, i);
String part2 = input.substring(i + 1);
List<Integer> resultPart1 = diffWaysToComputeSlow(part1);
List<Integer> resultPart2 = diffWaysToComputeSlow(part2);
// when either on is empty, nothing will be added to result
for (int res1 : resultPart1) {
for (int re2 : resultPart2) {
switch (input.charAt(i)) {
case '+':
result.add(res1 + re2);
break;
case '-':
result.add(res1 - re2);
break;
case '*':
result.add(res1 * re2);
break;
}
}
}
}
}
// if nothing is added to result list, it means the string is a number
if (result.isEmpty())
result.add(Integer.valueOf(input));
return result;
}
// use map to store intermediate results to speed up recursion
Map<String, List<Integer>> resultCache = new HashMap<>();
//1 ms 100% 34.2 MB 100%
public List<Integer> diffWaysToCompute(String input) {
if (resultCache.containsKey(input))
return resultCache.get(input);
List<Integer> result = new LinkedList<>();
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') {
String part1 = input.substring(0, i);
String part2 = input.substring(i + 1);
List<Integer> resultPart1 = diffWaysToCompute(part1);
List<Integer> resultPart2 = diffWaysToCompute(part2);
// when either on is empty, nothing will be added to result
for (int res1 : resultPart1) {
for (int re2 : resultPart2) {
//using swith here is not very efficient
// 78.94% 2 ms 100.00% 34.2 MB
// switch (input.charAt(i)) {
// case '+':
// result.add(res1 + re2);
// break;
// case '-':
// result.add(res1 - re2);
// break;
// case '*':
// result.add(res1 * re2);
// break;
// }
if (input.charAt(i) == '+')
result.add(res1 + re2);
else if (input.charAt(i) == '-')
result.add(res1 - re2);
else
result.add(res1 * re2);
}
}
}
}
// if nothing is added to result list, it means the string is a number
if (result.isEmpty())
result.add(Integer.valueOf(input));
resultCache.put(input, result);
return result;
}
}