-
Notifications
You must be signed in to change notification settings - Fork 2
[이예슬] 3주차 문제풀이 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[이예슬] 3주차 문제풀이 #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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("+"); | ||
| } | ||
|
|
||
| const poppedNum = stack.pop(); | ||
| answerStack.push("-"); | ||
|
|
||
| if (poppedNum !== targetNum) { | ||
| answerStack.length = 0; | ||
| answerStack.push("NO"); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| console.log(answerStack.join("\n")); | ||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 변수를 사용하지 않을 수 있습니다. |
||
|
|
||
| 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 ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+, -, NO 같은 문자도 상수로 관리해도 좋을 것 같습니다.
#9 (comment)