๋ฌธ์ ๋งํฌ
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ์์ ์ต๋ํ
IT ๋ฒค์ฒ ํ์ฌ๋ฅผ ์ด์ํ๊ณ ์๋ ๋ผ์ด์ธ์ ๋งค๋ ์ฌ๋ด ํด์ปคํค ๋ํ๋ฅผ ๊ฐ์ตํ์ฌ ์ฐ์น์์๊ฒ ์๊ธ์ ์ง๊ธํ๊ณ ์์ต๋๋ค. ์ด๋ฒ ๋ํ์์๋ ์ฐ์น์์๊ฒ ์ง๊ธ๋๋ ์๊ธ์ ์ด์ ๋ํ์๋ ๋ค๋ฅด๊ฒ ๋ค์๊ณผ
programmers.co.kr
์ ๊ทผ ๋ฐฉ๋ฒ
1. `expression = "100-200*300-500+20"` ๊ฐ์ด ๋ฌธ์์ด๋ก ๋์ด์๋ ์์ ์ซ์ + ์ฐ์ฐ์ ํํ์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ผ ํ๋ค. => ์ ๊ท์ ์ฌ์ฉ
const re = /[-*+]/g; const operationArr = expression.match(re); // [๋ชจ๋ ์ฐ์ฐ์ ์ถ๋ ฅ] const numArr = expression.split(re).map(e => parseInt(e)); // [๋ชจ๋ ์ซ์ ์ถ๋ ฅ]โ
2. ์ฐ์ฐ์์ ์ฐ์ ์์์ ์์ด์ ๊ตฌํด์ผํ๋ค. (์ต๋ ์ฐ์ฐ์์ ์ข ๋ฅ๊ฐ 3๊ฐ์ด๋ฏ๋ก 3!์ด ์ต๋)
// ์ฌ๊ท๋ฅผ ์ด์ฉํ์ฌ ์์ด์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์ ๋์ถ const getPermutations = function (arr, selectNumber) { const results = []; if (selectNumber === 1) return arr.map((el) => [el]); arr.forEach((fixed, index, origin) => { const rest = [...origin.slice(0, index), ...origin.slice(index+1)] const permutations = getPermutations(rest, selectNumber - 1); const attached = permutations.map((el) => [fixed, ...el]); results.push(...attached); }); return results; }โ
์์ด๊ณผ ์กฐํฉ์ ์ ์ค๋ช ํ ๋ธ๋ก๊ทธ
3. ์ฐ์ ์์๋ฅผ ์ด์ฉํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ๋์ถํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.1. ์์ ๊ตฌํ numArr๊ณผ operationArr์ ์ถ๋ ฅํด๋ณด๋ฉด (expression = "100-200*300-500+20" ์ธ ๊ฒฝ์ฐ)
์ฐ์ฐ์์ ํผ์ฐ์ฐ์์ index ๋ฒํธ๊ฐ ๊ฐ์ ๊ฒ์ ์ ์ ์๋ค.
์ด๋ฅผ ์ด์ฉํ์ฌ operationArr === "์ฐ์ ์์๊ฐ ๋์ ์ฐ์ฐ์" ์ผ๋
numArr[index], numArr[index+1] ๊ณผ operationArr[index]๋ฅผ ์ฐ์ฐํ๋ฉด ์ฐ์ ์์๋๋ก ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ splice()๋ฅผ ํตํด ์ฐ์ฐ์ด ๋๋ element๋ฅผ ์ง์์ฃผ๋ฉด ์ํ๋ ๊ฐ์ ์ป์ ์ ์๋ค.
์ ์ฝ๋๋ ์ฐ์ ์์๊ฐ ("*", "+", "-") ์ธ ๊ฒฝ์ฐ๋ฅผ ์์๋ก ์ ํด์ ๊ตฌํ ๊ฒฐ๊ณผ์ด๋ค.
๋ฐฐ์ด ๋ถ๋ถ
1. operationResult์์ operation์ ๋ฌธ์์ธ oper1, oper2, oper3์ผ๋ก ๋ฐ์๋๋ฐ,
์ด๋ฅผ ๋ค์ numArr[i] oper1 numArr[i+1] ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฐ์ฐ์๋ก ์นํํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ๋ค.
=> eval() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฌธ์๋ฅผ ์ฐ์ฐ์๋ก ์ฌ์ฉํ ์ ์์๋ค.
2. operationResult(numArr, operationArr, oper1, oper2, oper3)๋ก ํธ์ถํ๋ฉด ์๋ณธ ๋ฐฐ์ด์ธ numArr, operationArr์ด ๋ณํด์ permutationOper์ ๊ฐ๋ค์ ๋ชจ๋ ์ํํ ๋ ์ํ๋ ๊ฐ์ ์ป์ ์ ์์๋ค.
=> spread ์ฐ์ฐ์๋ฅผ ํตํด ์์ ๋ณต์ฌ๋ฅผ ํ์ฌ์ ์๋ณธ ๋ฐฐ์ด์ ์ํฅ์ ์ฃผ์ง ์๋๋ก operationResult ํจ์๋ฅผ ํธ์ถํ์๋ค.
3. ์ฒ์ ๊ตฌํํ ๋ก์ง์ ์ฐ์ฐ์๊ฐ 3๊ฐ ๋ชจ๋ ์กด์ฌํ ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ก์๋ค.
ํ์ง๋ง ์ฐ์ฐ์๊ฐ ํ๋์ธ ๊ฒฝ์ฐ, ๋๊ฐ์ธ ๊ฒฝ์ฐ๋ ์์ด์ ์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด
oper2 !== undefined, oper3 !== undefined ์กฐ๊ฑด์ ์ถ๊ฐํ์๋ค.
๋ํ operationResult(oper1, oper2, oper3)์ operationResult( ...e)์ผ๋ก ํธ์ถ ํ ์ ์๋ค๋ ์ ๋ ๋ฐฐ์ ๋ค.
(permutationOper.forEach(e => )์์ ๋ง์ฝ e์ ๊ธธ์ด๊ฐ 2๋ผ๋ฉด oper1, oper2, ๊ธธ์ด๊ฐ 1์ด๋ผ๋ฉด oper1๋ง ํธ์ถํ๊ณ ๋๋จธ์ง๋ undefined๋ก ํ ๋นํ๋ค.)
function solution(expression) {
var answer = [];
const re = /[-*+]/g;
const operationArr = expression.match(re);
const numArr = expression.split(re).map(e => parseInt(e));
let uniqueOper = new Set(operationArr);
uniqueOper = Array.from(uniqueOper);
let permutationOper = getPermutations(uniqueOper, uniqueOper.length)
permutationOper.forEach(e => {
answer.push(operationResult([...numArr], [...operationArr], ...e))
})
return Math.max(...answer);
}
const operationResult = (numArr, operationArr, oper1, oper2, oper3) =>{
let result = 0;
for(let i = 0; i <= operationArr.length; i++){
if(operationArr[i] === oper1){
result = eval(numArr[i] + oper1 + numArr[i+1]);
numArr.splice(i,2,result);
operationArr.splice(i,1);
i = -1;
result = 0;
continue;
}else if(!operationArr.includes(oper1) && operationArr[i] === oper2 && oper2 !== undefined){
result = eval(numArr[i] + oper2 + numArr[i+1]);
numArr.splice(i,2,result);
operationArr.splice(i,1);
i = -1;
result = 0;
continue;
}else if(!operationArr.includes(oper2) && operationArr[i] === oper3 && oper3 !== undefined){
result = eval(numArr[i] + oper3 + numArr[i+1]);
numArr.splice(i,2,result);
operationArr.splice(i,1);
i = -1;
result = 0;
continue;
}
}
return Math.abs(numArr[0]);
}
const getPermutations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((el) => [el]);
arr.forEach((fixed, index, origin) => {
const rest = [...origin.slice(0, index), ...origin.slice(index+1)]
const permutations = getPermutations(rest, selectNumber - 1);
const attached = permutations.map((el) => [fixed, ...el]);
results.push(...attached);
});
return results;
}
'์ฝ๋ฉํ ์คํธ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Javascript] ์์ ๋ง๋ค๊ธฐ (12977) (0) | 2022.06.10 |
---|---|
[Javascript] 2016๋ (12901) (0) | 2022.06.06 |
[Javscript] [1์ฐจ] ๋น๋ฐ์ง๋ (17681) (0) | 2022.05.31 |
[Javascript] ์ผ๊ฐ ๋ฌํฝ์ด (68645) (0) | 2022.05.29 |
[Javascript] ๊ดํธ ํ์ ํ๊ธฐ (76502) (0) | 2022.05.27 |