[2020 KAKAO INTERNSHIP Level 2] 수식 최대화
1분 읽기
풀이 1: DFS
function solution(expression) {
const operators = Array.from(new Set(expression.match(/[\+|\-|\*]/g)));
const visited = new Array(operators.length).fill(false);
const cases = new Array();
let max = 0;
let splitted = expression.match(/([0-9]+)|([\+|\-|\*])/g);
function calc(priority){
let current = [...splitted];
// ✅ 연산자 우선순위에 따라 계산 수행 → 최대 연산자 종류(3) * 연산자와 피연산자 최대 길이(100)
priority.forEach(operator => {
// 현재 우선순위를 기준으로 계산 → 배열 상태 저장
const next = [];
for(let i = 0; i < current.length; i++){
if(current[i] !== operator){
next.push(current[i]);
continue;
}
i++;
const n1 = Number(next.pop());
const n2 = Number(current[i]);
if(operator === '+') next.push(n1 + n2);
if(operator === '-') next.push(n1 - n2);
if(operator === '*') next.push(n1 * n2);
}
current = next;
});
max = Math.max(max, Math.abs(current.pop()));
}
function getPriority(operators, priority = []){
if(priority.length === operators.length){
cases.push(priority);
return;
}
operators.forEach((o, i) => {
if(visited[i]) return;
visited[i] = true;
getPriority(operators, [...priority, o]);
visited[i] = false;
})
}
getPriority(operators); // 3! = 6번 연산
cases.forEach(calc); // 3! * 300 → 최대 1,800번 연산
return max;
}DFS/BFS10편 중 9번째
관련 글
4분 읽기
소수 구하기
소수 판별법과 소수를 구하는 알고리즘을 정리합니다.
1분 읽기
[프로그래머스 Level 2] 미로 탈출
프로그래머스 미로 탈출을 BFS로 풀이합니다.
1분 읽기
[프로그래머스 Level 2] 마법의 엘리베이터
프로그래머스 마법의 엘리베이터를 자리수 그리디로 풀이합니다.