Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add (programmers - level2 2문제): js 풀이
- Loading branch information
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function solution(m, n, board) { | ||
let answer = 0; | ||
board = board.map(item => item.split("")); | ||
|
||
while (true) { | ||
const visited = Array.from({ length: m }, (v, i) => | ||
Array.from({ length: n }, (v, i) => false) | ||
); | ||
for (let i = 0; i < m; i++) { | ||
for (let j = 0; j < n; j++) { | ||
if (j + 1 < n && i + 1 < m) { | ||
const current = board[i][j]; | ||
if (current !== 0) { | ||
const right = board[i][j + 1]; | ||
const bottom = board[i + 1][j]; | ||
const right_bottom = board[i + 1][j + 1]; | ||
|
||
if ( | ||
current === right && | ||
current === bottom && | ||
current === right_bottom | ||
) { | ||
visited[i][j] = true; | ||
visited[i][j + 1] = true; | ||
visited[i + 1][j] = true; | ||
visited[i + 1][j + 1] = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let cnt = 0; | ||
for (let i = 0; i < m; i++) { | ||
for (let j = 0; j < n; j++) { | ||
if (visited[i][j]) { | ||
cnt++; | ||
if (i > 0) { | ||
for (let k = i; k > 0; k--) { | ||
board[k][j] = board[k - 1][j]; | ||
board[k - 1][j] = 0; | ||
} | ||
} else { | ||
board[i][j] = 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (cnt === 0) { | ||
break; | ||
} | ||
|
||
answer += cnt; | ||
} | ||
|
||
return answer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const getFilePart = file => { | ||
const name = file.split(""); | ||
const numStartIdx = name.findIndex( | ||
s => s !== " " && Number.isInteger(Number(s)) | ||
); | ||
const head = name.slice(0, numStartIdx).join("").toUpperCase(); | ||
const numEndIdx = name | ||
.slice(numStartIdx, name.length) | ||
.findIndex((s, idx) => s === " " || !Number.isInteger(Number(s))); | ||
const number = Number( | ||
name | ||
.slice( | ||
numStartIdx, | ||
numStartIdx + (numEndIdx === -1 || numEndIdx >= 6 ? 5 : numEndIdx) | ||
) | ||
.join("") | ||
); | ||
|
||
return { | ||
head, | ||
number, | ||
}; | ||
}; | ||
|
||
function solution(files) { | ||
var answer = []; | ||
files.sort((a, b) => { | ||
const fileA = getFilePart(a); | ||
const fileB = getFilePart(b); | ||
|
||
if (fileA.head !== fileB.head) { | ||
return fileA.head.localeCompare(fileB.head); | ||
} else { | ||
if (fileA.number !== fileB.number) { | ||
return fileA.number - fileB.number; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
}); | ||
return files; | ||
} |