Skip to content

Commit

Permalink
add (programmers - level2 2문제): js 풀이
Browse files Browse the repository at this point in the history
  • Loading branch information
padosum committed Feb 8, 2023
1 parent 7f4f475 commit 18a64fa
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
58 changes: 58 additions & 0 deletions programmers/level2/[1차] 프렌즈4블록.js
@@ -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;
}
42 changes: 42 additions & 0 deletions programmers/level2/[3차] 파일명 정렬.js
@@ -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;
}

0 comments on commit 18a64fa

Please sign in to comment.