Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lys/스택 수열.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

//const input = fs.readFileSync("/dev/stdin").toString().trim().split('\n').map(Number);
const fs = require('fs');
const input = fs.readFileSync('lys/input.txt', 'utf-8').trim().split('\n').map(Number);
const n = input.shift();
const answerStack = [];
const stack = [];
let num = 1;

for (let i = 0; i < n; i++) {
const targetNum = input[i];

while (num <= targetNum) {
stack.push(num);
num++;
answerStack.push("+");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+, -, NO 같은 문자도 상수로 관리해도 좋을 것 같습니다.
#9 (comment)

}

const poppedNum = stack.pop();
answerStack.push("-");

if (poppedNum !== targetNum) {
answerStack.length = 0;
answerStack.push("NO");
break;
}
}

console.log(answerStack.join("\n"));
56 changes: 56 additions & 0 deletions lys/프렌즈 4블럭.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function solution(m, n, board) {
let removedBlocks = [];
const Board = [];
let index = 0;
let totalRemoved = 0;
Comment on lines +4 to +5

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적으로 자바스크립트로 알고리즘 문제를 풀 때도 let사용을 지양하려고 하는데요, let을 사용하다보면 같은 스코프 내부라면 어디서든 수정이 가능하므로 문제를 틀렸을 때 디버깅하기가 힘들어진다고 생각하기 때문입니다. 함수를 추가로 만들고 그 함수의 반환값을 const로 선언해서 필요할 때만 사용해보는 것도 좋을 것 같습니다.
예를 들어, 이 문제에서 구해야 하는 값이 총 파괴된 블럭의 수이므로

if (removedBlocks.length === 0) break;

if (removedBlocks.length === 0) return 파괴된블럭의수를센다(Board);

로 변경하면 totalRemoved 변수를 사용하지 않을 수 있습니다.
const를 사용해서 더 선언적으로 코드를 작성하는 것을 알고리즘 문제 풀이를 통해서 학습해보기 좋다고 생각합니다.
추가로 절차와 선언의 차이를 학습해보셔도 좋을 것 같네요:)


board.forEach((row) => Board.push(row.split("")));

while (true) {
for (let i = 0; i < m - 1; i++) {
for (let j = 0; j < n; j++) {
if (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분을 함수로 쪼개서 적용하면 더 읽기 쉬울 것 같아요!

Board[i][j] !== '0' &&
Board[i][j] === Board[i][j + 1] &&
Board[i][j + 1] === Board[i + 1][j] &&
Board[i + 1][j] === Board[i + 1][j + 1]
Comment on lines +13 to +16

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바스크립트 Array.prototype.every를 적용해보면 더 깔끔하고 선언적인 코드를 만들 수 있을 것 같습니다 :)

) {
removedBlocks.push([i, j], [i, j + 1], [i + 1, j], [i + 1, j + 1]);
}
}
}

if (removedBlocks.length === 0) break;

removedBlocks.forEach((block) => (Board[block[0]][block[1]] = '0'));
removedBlocks = [];

for (let i = 1; i < m; i++) {
for (let j = 0; j < n; j++) {
if (Board[i][j] === '0') {
Board[i][j] = Board[i - 1][j];

if (i - 2 >= 0 && Board[i - 2][j] !== '0') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

들여쓰기가 깊어질 수록
상위에 선언된 명령을 모두 기억해야 하위 코드를 이해할 수 있게 되기 때문에,
하위 코드를 이해하기 위한 부담이 커집니다..!
코테에서 빠른 시간안에 문제를 푸는 것도 중요하지만,
함수로 쪼개서 구현하는 것도 디버깅에 매우 유용해서
쪼개서 구현하는게 추후에 더 좋을 것 같습니다!

index = i;
while (index - 2 >= 0) {
Board[index - 1][j] = Board[index - 2][j];
Board[index - 2][j] = '0';
index--;
}
} else {
Board[i - 1][j] = '0';
}
}
}
}
}
Comment on lines +28 to +46

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

당장 수정할 수 있는 부분은 아니라고 생각되지만, 들여쓰기는 적을수록 좋습니다.
시간복잡도가 예측이 안되고 가독성이 떨어지기 때문입니다.
그래도 로직이 복잡하면 어쩔 수 없죠. 그냥 고민한번 해보세요.
클린코드에도 작게 만들라는 이야기가 있던것같네요.


for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (Board[i][j] === '0') {
totalRemoved++;
}
}
}
return totalRemoved;
}