Skip to content

Commit 59d69a4

Browse files
committed
Add: algorithm of programmers
1 parent 1dd27f1 commit 59d69a4

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package io.github.imsejin.study.programmers;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Objects;
7+
import java.util.function.LongBinaryOperator;
8+
9+
/**
10+
* <a href="https://school.programmers.co.kr/learn/courses/30/lessons/67257">수식 최대화</a>
11+
*/
12+
public class L67257 {
13+
14+
static long solve(String expression) {
15+
List<Operation> Operation = parse(expression);
16+
List<Operation> items;
17+
18+
long max = 0;
19+
20+
// Permutation.
21+
Operator[] operators = Operator.values();
22+
for (int i = 0; i < operators.length; i++) {
23+
for (int j = 0; j < operators.length; j++) {
24+
for (int k = 0; k < operators.length; k++) {
25+
if (i == j || j == k || i == k) continue;
26+
27+
items = new LinkedList<>(Operation);
28+
29+
calculate(items, operators[i]);
30+
calculate(items, operators[j]);
31+
calculate(items, operators[k]);
32+
33+
long result = Math.abs(items.get(0).operand);
34+
if (max < result) max = result;
35+
}
36+
}
37+
}
38+
39+
return max;
40+
}
41+
42+
private static void calculate(List<Operation> operations, Operator operator) {
43+
int index;
44+
while (true) {
45+
index = findIndex(operations, operator);
46+
if (index == -1) break;
47+
48+
long left = operations.get(index - 1).operand;
49+
long right = operations.get(index + 1).operand;
50+
51+
long result = operator.applyAsLong(left, right);
52+
53+
// Remove used operands and an operator from operations.
54+
operations.remove(index - 1);
55+
operations.remove(index - 1);
56+
operations.remove(index - 1);
57+
operations.add(index - 1, Operation.from(result));
58+
}
59+
}
60+
61+
private static int findIndex(List<Operation> operations, Operator operator) {
62+
int size = operations.size();
63+
64+
for (int i = 0; i < size; i++) {
65+
Operation operation = operations.get(i);
66+
if (Objects.equals(operation.operator, operator)) return i;
67+
}
68+
69+
return -1;
70+
}
71+
72+
private static List<Operation> parse(String expression) {
73+
String[] strings = expression.split("(?<=[0-9])(?=[+*-])|(?<=[+*-])(?=[0-9])");
74+
List<Operation> operations = new ArrayList<>();
75+
76+
for (String s : strings) {
77+
Operation operation = Operation.from(s);
78+
operations.add(operation);
79+
}
80+
81+
return operations;
82+
}
83+
84+
// -------------------------------------------------------------------------------------------------
85+
86+
private static class Operation {
87+
private final Long operand;
88+
private final Operator operator;
89+
90+
private Operation(Long operand, Operator operator) {
91+
this.operand = operand;
92+
this.operator = operator;
93+
}
94+
95+
public static Operation from(String item) {
96+
return switch (item) {
97+
case "+" -> new Operation(null, Operator.PLUS);
98+
case "-" -> new Operation(null, Operator.MINUS);
99+
case "*" -> new Operation(null, Operator.MULTIPLY);
100+
default -> new Operation(Long.parseLong(item), null);
101+
};
102+
}
103+
104+
public static Operation from(long operand) {
105+
return new Operation(operand, null);
106+
}
107+
}
108+
109+
private enum Operator implements LongBinaryOperator {
110+
PLUS {
111+
@Override
112+
public long applyAsLong(long left, long right) {
113+
return left + right;
114+
}
115+
},
116+
117+
MINUS {
118+
@Override
119+
public long applyAsLong(long left, long right) {
120+
return left - right;
121+
}
122+
},
123+
124+
MULTIPLY {
125+
@Override
126+
public long applyAsLong(long left, long right) {
127+
return left * right;
128+
}
129+
}
130+
}
131+
132+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.imsejin.study.programmers
2+
3+
import spock.lang.Specification
4+
5+
class L67257Spec extends Specification {
6+
7+
def "test"() {
8+
when:
9+
def actual = L67257.solve(expression)
10+
11+
then:
12+
actual == expected
13+
14+
where:
15+
expression | expected
16+
"100-200*300-500+20" | 60420
17+
"50*6-3*2" | 300
18+
"0+0" | 0
19+
"999*999" | 998001
20+
"999*999*999*999*999*999*9" | 8946134820134946009
21+
}
22+
23+
}

0 commit comments

Comments
 (0)