-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKakao_Summer_2.java
More file actions
122 lines (103 loc) · 3.19 KB
/
Kakao_Summer_2.java
File metadata and controls
122 lines (103 loc) · 3.19 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package kakao_summer_2020;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Kakao_Summer_2 {
static int[] arr;
static char[] oper = {'*', '+', '-'};
static boolean priorityCheck;
static long maxValue = Long.MIN_VALUE;
public static void main(String[] args) {
String expr = "100-200*300-500+20";
long result = solution(expr);
System.out.println(result);
}
public static long solution(String expression) {
long answer = 0;
String expr = expression;
StringBuilder sb = new StringBuilder();
arr = new int[3];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
perm(arr, 3, 3, 0, expr);
answer = maxValue;
return answer;
}
public static int changeChartoNum(char oper) {
int result = -1;
switch(oper) {
case '*' :
result = 0;
break;
case '+' :
result = 1;
break;
case '-':
result = 2;
break;
}
return result;
}
public static void perm(int[] arr, int n, int r, int step, String expr) {
if(r == step) {
Stack<Character> operStack = new Stack();
StringBuilder numSb = new StringBuilder();
List<String> exprArr = new ArrayList<>();
// 중위 -> 후위
for (int i = 0; i < expr.length(); i++) {
if(expr.charAt(i) == '*' || expr.charAt(i) == '+' || expr.charAt(i) == '-') {
exprArr.add(numSb.toString());
numSb.setLength(0);
// 숫자 낮은 것이 우선 순위 더 높은걸로 취급
// 스택이 존재하면서 현재 연산자 우선순위 <= 스택 최상단 연산자 우선순위 일때 반복
while(!operStack.isEmpty() && (arr[changeChartoNum(expr.charAt(i))] <= arr[changeChartoNum(operStack.peek())])) {
exprArr.add(String.valueOf(operStack.pop()));
}
operStack.push(expr.charAt(i));
}
// 숫자일때
else {
numSb.append(expr.charAt(i));
}
}
exprArr.add(numSb.toString());
while(!operStack.isEmpty()) {
exprArr.add(String.valueOf(operStack.pop()));
}
Stack<Long> calculStack = new Stack<>();
for (int i = 0; i < exprArr.size(); i++) {
if(exprArr.get(i).equals("*") || exprArr.get(i).equals("+") || exprArr.get(i).equals("-")) {
long secondNum = calculStack.pop();
long firstNum = calculStack.pop();
switch(exprArr.get(i)) {
case "*" :
calculStack.push(firstNum * secondNum);
break;
case "+" :
calculStack.push(firstNum + secondNum);
break;
case "-" :
calculStack.push(firstNum - secondNum);
break;
}
}
else {
calculStack.push(Long.valueOf(exprArr.get(i)));
}
}
maxValue = Math.max(maxValue, Math.abs(calculStack.pop()));
}
else {
for (int i = step; i < n; i++) {
int temp = arr[i];
arr[i] = arr[step];
arr[step] = temp;
perm(arr, n, r, step+1, expr);
temp = arr[i];
arr[i] = arr[step];
arr[step] = temp;
}
}
}
}