Skip to content

Commit 7a0b3c2

Browse files
committed
nsun9505 solved 수식 최대화
1 parent 0214dd9 commit 7a0b3c2

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# [2020 카카오 인턴십] 수식 최대화 - JAVA
2+
3+
## 분류
4+
> 스택
5+
>
6+
> 순열
7+
8+
## 코드
9+
```java
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.HashSet;
13+
import java.util.Stack;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
public class Solution {
18+
static HashMap<String, Integer> opMap = new HashMap<>();
19+
static Stack<String> stack = new Stack<>();
20+
static ArrayList<String> list = new ArrayList<>();
21+
static ArrayList<String> exp = new ArrayList<>();
22+
static String[] operators;
23+
static long ans = 0;
24+
public long solution(String expression) {
25+
Pattern pattern = Pattern.compile("\\d{1,3}");
26+
Matcher matcher = pattern.matcher(expression);
27+
28+
while(matcher.find())
29+
list.add(matcher.group());
30+
31+
pattern = Pattern.compile("\\D");
32+
matcher = pattern.matcher(expression);
33+
HashSet<String> opSet = new HashSet<>();
34+
int idx = 0;
35+
while(matcher.find()) {
36+
String op = matcher.group();
37+
opSet.add(op);
38+
exp.add(list.get(idx++));
39+
exp.add(op);
40+
}
41+
exp.add(list.get(idx));
42+
43+
operators = new String[opSet.size()];
44+
idx = 0;
45+
for(String op : opSet)
46+
operators[idx++] = op;
47+
48+
perm(0);
49+
50+
return ans;
51+
}
52+
53+
public void perm(int depth){
54+
if(depth == operators.length){
55+
for(int i=0; i<operators.length; i++)
56+
opMap.put(operators[i], i);
57+
58+
list.clear();
59+
stack.clear();
60+
for(int i=0; i<exp.size(); i++){
61+
String str = exp.get(i);
62+
if(str.equals("+") || str.equals("-") || str.equals("/") || str.equals("*")){
63+
while(!stack.isEmpty()){
64+
if(opMap.get(stack.peek()) < opMap.get(str))
65+
break;
66+
list.add(stack.pop());
67+
}
68+
stack.push(str);
69+
} else {
70+
list.add(str);
71+
}
72+
}
73+
74+
while(!stack.isEmpty())
75+
list.add(stack.pop());
76+
77+
for(String str : list){
78+
if(str.equals("+") || str.equals("-") || str.equals("/") || str.equals("*")){
79+
long op2 = Long.parseLong(stack.pop());
80+
long op1 = Long.parseLong(stack.pop());
81+
82+
if(str.equals("+"))
83+
stack.push(String.valueOf(op1 + op2));
84+
else if(str.equals("-"))
85+
stack.push(String.valueOf(op1 - op2));
86+
else if(str.equals("*"))
87+
stack.push(String.valueOf(op1 * op2));
88+
else
89+
stack.push(String.valueOf(op1 / op2));
90+
} else {
91+
stack.push(str);
92+
}
93+
}
94+
95+
long sum = Math.abs(Long.parseLong(stack.pop()));
96+
ans = Math.max(ans, sum);
97+
98+
return;
99+
}
100+
101+
for(int i=depth; i<operators.length; i++){
102+
swap(depth, i);
103+
perm(depth+1);
104+
swap(depth, i);
105+
}
106+
}
107+
108+
public void swap(int x, int y){
109+
String tmp = operators[x];
110+
operators[x] = operators[y];
111+
operators[y] = tmp;
112+
}
113+
}
114+
```
115+
116+
## 문제 풀이
117+
먼저 연산자와 피연산자를 알아오기 위해서 정규 표현식을 사용해서 파싱했습니다.
118+
- "\\d" : 숫자만 가져오기
119+
- "\\D" : 숫자가 아닌 애들 가져오기
120+
121+
순열을 사용해서 연산자의 순서를 정하고 정해진 순서에 따라 Map에 우선순위를 저장하였습니다.
122+
123+
계산하는 방법은 중위 표현식을 후위표현식으로 변경하여 계산하였습니다.
124+
125+
## 후기
126+
후위 표현식과 순열을 사용하면 쉽게 풀 수 있는 문제였습니다!
127+
128+
후위 표현식은 코드 안 보고 짜봤습니다!
129+
130+
싸피가 도움이 되긴 하네요..ㅎ
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.HashSet;
4+
import java.util.Stack;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public class Solution {
9+
static HashMap<String, Integer> opMap = new HashMap<>();
10+
static Stack<String> stack = new Stack<>();
11+
static ArrayList<String> list = new ArrayList<>();
12+
static ArrayList<String> exp = new ArrayList<>();
13+
static String[] operators;
14+
static long ans = 0;
15+
public long solution(String expression) {
16+
Pattern pattern = Pattern.compile("\\d{1,3}");
17+
Matcher matcher = pattern.matcher(expression);
18+
19+
while(matcher.find())
20+
list.add(matcher.group());
21+
22+
pattern = Pattern.compile("\\D");
23+
matcher = pattern.matcher(expression);
24+
HashSet<String> opSet = new HashSet<>();
25+
int idx = 0;
26+
while(matcher.find()) {
27+
String op = matcher.group();
28+
opSet.add(op);
29+
exp.add(list.get(idx++));
30+
exp.add(op);
31+
}
32+
exp.add(list.get(idx));
33+
34+
operators = new String[opSet.size()];
35+
idx = 0;
36+
for(String op : opSet)
37+
operators[idx++] = op;
38+
39+
perm(0);
40+
41+
return ans;
42+
}
43+
44+
public void perm(int depth){
45+
if(depth == operators.length){
46+
for(int i=0; i<operators.length; i++)
47+
opMap.put(operators[i], i);
48+
49+
list.clear();
50+
stack.clear();
51+
for(int i=0; i<exp.size(); i++){
52+
String str = exp.get(i);
53+
if(str.equals("+") || str.equals("-") || str.equals("/") || str.equals("*")){
54+
while(!stack.isEmpty()){
55+
if(opMap.get(stack.peek()) < opMap.get(str))
56+
break;
57+
list.add(stack.pop());
58+
}
59+
stack.push(str);
60+
} else {
61+
list.add(str);
62+
}
63+
}
64+
65+
while(!stack.isEmpty())
66+
list.add(stack.pop());
67+
68+
for(String str : list){
69+
if(str.equals("+") || str.equals("-") || str.equals("/") || str.equals("*")){
70+
long op2 = Long.parseLong(stack.pop());
71+
long op1 = Long.parseLong(stack.pop());
72+
73+
if(str.equals("+"))
74+
stack.push(String.valueOf(op1 + op2));
75+
else if(str.equals("-"))
76+
stack.push(String.valueOf(op1 - op2));
77+
else if(str.equals("*"))
78+
stack.push(String.valueOf(op1 * op2));
79+
else
80+
stack.push(String.valueOf(op1 / op2));
81+
} else {
82+
stack.push(str);
83+
}
84+
}
85+
86+
long sum = Math.abs(Long.parseLong(stack.pop()));
87+
ans = Math.max(ans, sum);
88+
89+
return;
90+
}
91+
92+
for(int i=depth; i<operators.length; i++){
93+
swap(depth, i);
94+
perm(depth+1);
95+
swap(depth, i);
96+
}
97+
}
98+
99+
public void swap(int x, int y){
100+
String tmp = operators[x];
101+
operators[x] = operators[y];
102+
operators[y] = tmp;
103+
}
104+
}

0 commit comments

Comments
 (0)