All'alba vincerò

At dawn, I will win!

Algorithm

[백준] 10828번: 스택

나디아 Nadia 2024. 9. 30. 15:01

✏️ 개념 공부

 

스택(stack)

 

Algorithm/Algorithm/CS-Theory/Data-Structures/Stack & Queue.md at main · kwonboryong/Algorithm

알고리즘(algorithm) 문제 풀이 스터디 . Contribute to kwonboryong/Algorithm development by creating an account on GitHub.

github.com

 

 

큐(queue)

 

Algorithm/Algorithm/CS-Theory/Data-Structures/queue.js at main · kwonboryong/Algorithm

알고리즘(algorithm) 문제 풀이 스터디 . Contribute to kwonboryong/Algorithm development by creating an account on GitHub.

github.com

 

 


🛠️ 문제 

10828번 스택

 

 

 

 

💡 풀이

const fs = require('fs');
const path = require('path');

const input = fs.readFileSync(path.join(__dirname, 'input.txt')).toString().trim().split('\n');


const stack = [];
const result = [];

const len = parseInt(input[0]); 
// 입력 파일(input.txt)의 첫 번째 줄의 명령어 개수

for (let i = 1; i <= len; i++) {
  const command = input[i].trim().split(' ');
  // 명령어 나누기 (ex. ['push', '3'])

  switch (command[0]) {
    case 'push':
      stack.push(parseInt(command[1])); 
      // 명령어의 숫자를 숫자로 변환해서 스택에 저장
      break;

    case 'pop':
      result.push(stack.pop() || -1); 
      // pop 결과 저장 || 빈 배열이면 -1
      break;

    case 'size':
      result.push(stack.length); 
      // 현재 스택 크기의 크기
      break;

    case 'empty':
      result.push(stack.length === 0 ? 1 : 0); 
      // 빈 배열이면 1(true) || 아니면 0(false)
      break;

    case 'top':
      result.push(stack.length ? stack[stack.length - 1] : -1); 
      // 안 비어있으면 스택의 끝 요소 저장 || 비어있으면 -1 
      break;
  }
}

console.log(result.join('\n'));
// 결과를 줄바꿈해서 출력

 

 

Algorithm/Algorithm/Solving/Stack & Que/10828.js at main · kwonboryong/Algorithm

알고리즘(algorithm) 문제 풀이 스터디 . Contribute to kwonboryong/Algorithm development by creating an account on GitHub.

github.com

 

 


💁‍♀️ 회고

드디어 알고리즘 공부 시작...!

 

스택은 간단해서 금방 구현했으나 백준에서 자바스크립트가 안돼서 node.js에서 사용할 수 있도록 바꾸느라 애먹었다.

프로그래머스가 확실히 편한데 진행 중인 알고리즘 스터디에서 백준 기준으로 문제를 선정해주셔서 한동안은 백준에서 구현해야 하는데 벌써 눈 앞이 깜깜하다. 

 

이전에는 알고리즘 하면 겁부터 나서 이론만 공부하고 문제는 풀 엄두도 나지 않았는데 역시 유료 스터디를 진행하니 뭐든 풀게 된다.

앞으로도 화이팅

 

 

 

1주차 - 1일 | Notion

💡문제 분석 요약

thin-brisket-ae4.notion.site