https://www.acmicpc.net/problem/10828
const filePath = process.platform === 'linux' ? 0 : './input.txt';
let [N, ...commands] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
const stack = [];
let result = ''
commands.forEach(command => {
const log = run(command);
if (log !== undefined) result += log + '\n'
})
function run(command) {
let [cmd, elt] = command.split(' ');
switch(cmd){
case 'push':
stack.push(elt);
break;
case 'pop':
return run('empty') ? -1 :stack.pop()
case 'size':
return stack.length
case 'empty':
return stack.length === 0 ? 1 : 0
case 'top':
return run('empty') ? -1 : stack[stack.length-1]
}
}
console.log(result)
empty 함수 반복 호출 최적화:
pop과 top 연산에서 run('empty')를 통해 empty 함수를 중복 호출하는 대신, stack.length의 상태를 직접 확인하면 성능을 더 향상할 수 있습니다.
명령어에 따른 조건문 최적화:
switch 구문에서 명령어를 조건 없이 case에 따라 분리하고 있지만, pop, size, empty, top 명령은 return을 통해 결과값을 반환하므로 undefined 체크 대신 result에 직접 push할 수 있습니다.
반복적인 연산을 최소화하기 위해 조건문 단순화:
예를 들어 empty의 결과를 활용하여 stack.pop()과 stack[stack.length - 1]을 한 번만 호출하는 식으로 개선할 수 있습니다.
메모리 최적화:
result 문자열 대신 result 배열을 사용해 push 연산을 통해 결과를 추가하고, 마지막에 join('\n')을 통해 출력하는 방식으로 처리하면 메모리를 조금 더 절약할 수 있습니다.
리팩토링:
코드가 보다 간결해지고 이해하기 쉬워지도록 run 함수에서 stack의 상태를 반환하는 코드만 남기고, 스택 연산 자체는 switch 구문 밖에서 호출하는 것이 좋습니다.
const filePath = process.platform === 'linux' ? 0 : './input.txt';
let [N, ...commands] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
const stack = [];
const result = [];
commands.forEach(command => {
const output = run(command);
if (output !== undefined) result.push(output);
});
function run(command) {
const [cmd, elt] = command.split(' ');
switch (cmd) {
case 'push':
stack.push(Number(elt));
break;
case 'pop':
return stack.length === 0 ? -1 : stack.pop();
case 'size':
return stack.length;
case 'empty':
return stack.length === 0 ? 1 : 0;
case 'top':
return stack.length === 0 ? -1 : stack[stack.length - 1];
}
}
console.log(result.join('\n'));
result 배열을 사용하여 출력할 결과를 누적하고, 마지막에 한 번에 출력합니다.run 함수의 switch 구문에서 pop과 top의 empty 조건 검사를 단일 조건으로 처리하여 중복 호출을 최소화했습니다.stack의 상태에 따라 결과를 반환하고, 필요한 경우에만 result 배열에 결과를 저장하는 구조로 개선하였습니다.empty 검사를 중복으로 하는 경우 성능에 영향을 줄 수 있으므로, 이러한 연산을 최소화하는 것이 중요합니다.result 배열을 활용한 최적화: 출력 시 반복적인 문자열 연결(+=)보다, 배열을 사용해 결과를 한 번에 출력하면 메모리를 절약할 수 있다는 점을 알게 되었습니다.switch 구문에서 명령어에 따라 stack의 상태를 확인하고 반환하는 방식을 리팩토링함으로써, 가독성을 높이고 함수의 역할을 명확히 할 수 있었습니다.