https://school.programmers.co.kr/learn/courses/30/lessons/154540
maps array๋ฅผ split
์ ํตํด ํ๋์ฉ ๋์ด์ newMap์ ์ ์ฅํด ์ค๋ค.solution์์ ์ ์ธ
ํด ์ค ๋ค BFS์ ๋ณด๋ด์ค์ผ ํ๋ค.Number๋ก ํ ๋ณํ
์ ํด์ค ๋ค food ๋ณ์์ ๋ํ๋ค.sort๋ฅผ ์ด์ฉํด ์ค๋ฆ์ฐจ์
์ผ๋ก ์ ๋ ฌํ ๋ค ์ถ๋ ฅํ๋ค.const solution = (maps) => {
let canLive = [];
const column = maps.length;
const row = maps[0].length;
const newMap = maps.map((element) => element.split(""));
const visited = Array(column)
.fill(false)
.map(() => Array(row).fill(false));
for (let i = 0; i < row; i++) {
for (let j = 0; j < column; j++) {
if (newMap[j][i] === "X") continue;
canLive.push(Bfs(i, j, column, row, newMap, visited));
}
}
if (canLive.length !== 0) {
const result = canLive.filter((element) => element >= 0);
result.sort(function (a, b) {
return a - b;
});
return result;
}
return [-1];
};
const Bfs = (x, y, column, row, maps, visited) => {
const dx = [1, 0, -1, 0];
const dy = [0, 1, 0, -1];
let food = 0;
const queue = [];
if (visited[y][x] === true) {
return -1;
}
food += Number(maps[y][x]);
visited[y][x] = true;
queue.push([x, y]);
while (queue.length > 0) {
let [cx, cy] = queue.shift();
for (let i = 0; i < 4; i++) {
let nx = cx + dx[i];
let ny = cy + dy[i];
if (nx < 0 || ny < 0 || nx >= row || ny >= column) continue;
else if (visited[ny][nx] === true) continue;
else if (maps[ny][nx] === "X") continue;
else {
food += Number(maps[ny][nx]);
queue.push([nx, ny]);
visited[ny][nx] = true;
}
}
}
return food;
};
https://school.programmers.co.kr/learn/courses/30/lessons/152995
const solution = (scores) => {
const wanho = scores[0];
scores.sort((a,b) => a[0] === b[0] ? a[1] - b[1] : b[0] - a[0]);
let answer = 1;
let maxScore = 0;
const wanhoSum = wanho[0] + wanho[1];
for(let score of scores){
if(score[1] < maxScore) {
// ํ๋ฝ๋์
if(score === wanho) return -1;
} else {
// ์ธ์ผ๋์
maxScore = Math.max(maxScore, score[1]);
if(score[0] + score[1] > wanhoSum){
answer++;
}
}
}
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/133500
const solution = (n, lightHouse) => {
const visited = new Array(n + 1).fill(false);
let result = 0;
while (lightHouse.length) {
const map = new Array(n + 1).fill().map((_) => []);
for (const el of lightHouse) {
const [a, b] = el;
map[a].push(b);
map[b].push(a);
}
// ํ์๋ฒ
map
.filter((element) => element.length === 1)
.forEach((element) => {
const [target] = element;
if (!visited[target]) {
visited[target] = true;
if (map[target].length !== 1) {
result += 1;
} else {
// ์์ชฝ ๋ชจ๋ ์๋ ๊ฒฝ์ฐ์ด๋ฏ๋ก ๋ํ๊ธฐ๊ฐ ๋๋ฒ์ผ์ด๋๋ค.
result += 0.5;
}
}
});
// ๊ฐ์ ์ด 1๊ฐ์ธ ์ฌ ๋ชจ๋ ์ ๊ฑฐ
lightHouse = lightHouse.filter((el) => {
const [a, b] = el;
return !visited[a] && !visited[b];
});
}
return result;
};