Skip to content

권해정 / 17주차 #24

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
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
27 changes: 27 additions & 0 deletions haejeong/week16/나무자르기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const [N, M] = [4, 7];
const trees = [20, 15, 10, 17];

const solution = (N, M, trees) => {
trees.sort((a, b) => a - b);
let start = 0;
let end = trees[N - 1];
let answer = Number.MIN_SAFE_INTEGER;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
let sum = 0;
for (let el of trees) {
if (el > mid) sum += el - mid;
}

if (sum >= M) {
if (mid > answer) answer = mid;
start = mid + 1;
} else {
end = mid - 1;
}
}

return answer;
};

console.log(solution(N, M, trees));
23 changes: 23 additions & 0 deletions haejeong/week16/랜선자르기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const [K, N] = [4, 11];
const lines = [802, 743, 457, 539];

const solution = (K, N, lines) => {
lines.sort((a, b) => a - b);
let left = 0;
let right = lines[K - 1];
let answer = Number.MIN_SAFE_INTEGER;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
let lineNum = lines.reduce((acc, cur) => acc + Math.floor(cur / mid), 0);

if (lineNum >= N) {
if (mid > answer) answer = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return answer;
};

console.log(solution(K, N, lines));
20 changes: 20 additions & 0 deletions haejeong/week16/예산.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 2512번 문제
const arr = [70, 80, 30, 40, 100];
const budget = 450;

const solution = (arr, budget) => {
arr.sort((a, b) => a - b);
let min = 1;
let max = arr[arr.length - 1];

while (min <= max) {
let mid = parseInt((min + max) / 2);
let sum = arr.reduce((acc, cur) => acc + (cur <= mid ? cur : mid), 0);
if (sum <= budget) min = mid + 1;
else max = mid - 1;
}

return max;
};

console.log(solution(arr, budget));
21 changes: 21 additions & 0 deletions haejeong/week17/경로찾기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const N = +input[0];
const graph = input.slice(1).map((v) => v.split(" ").map(Number));

const answer = [...Array(N)].map(() => Array(N).fill(0));
const DFS = (node, start, visited) => {
for (let i = 0; i < N; i++) {
if (graph[node][i] && !visited[i]) {
visited[i] = true;
answer[start][i] = 1;
DFS(i, start, visited);
}
}
};

for (let i = 0; i < N; i++) {
const visited = Array(N).fill(false);
DFS(i, i, visited);
}

console.log(answer.map((v) => v.join(" ")).join("\n"));
38 changes: 38 additions & 0 deletions haejeong/week17/안전영역.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const N = +input[0];
const areas = input.slice(1).map((v) => v.split(" ").map((v) => +v));

const offsetX = [0, 0, -1, 1];
const offsetY = [-1, 1, 0, 0];

const dfs = (x, y, height, visited) => {
offsetX.forEach((dx, i) => {
const nx = x + dx;
const ny = y + offsetY[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny]) {
visited[nx][ny] = true;
dfs(nx, ny, height, visited);
}
});
};

let maxCount = 0;
for (let height = 0; height <= 100; height++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

높이가 1 이상이라 초기값을 1로 두어도 괜찮을 것 같습니다 저도 0부터 시작했지만..😌

// 잠기는 높이를 0부터 100까지 증가시키면서 안전 영역의 개수의 최댓값 찾기
// 높이를 변경시킬 때마다 visited 갱신
let count = 0;
const visited = [...Array(N)].map((_, x) => [...Array(N)].map((_, y) => areas[x][y] <= height));
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
// 1. 물에 잠기지 않았고 2. 미방문한 지역에서 방문 여부를 갱신
if (!visited[i][j]) {
visited[i][j] = true;
dfs(i, j, height, visited);
count++;
}
}
}
maxCount = Math.max(maxCount, count);
}

console.log(maxCount);
34 changes: 34 additions & 0 deletions haejeong/week17/연결요소의개수.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
let input = fs
.readFileSync(filePath)
.toString()
.trim()
.split("\n")
.map((item) => item.split(" ").map(Number));

const [N, M] = input.shift();
const graph = [...Array(N + 1)].map(() => []);
const visited = [...Array(N + 1)].map(() => []).fill(false);
input.forEach(([from, to]) => {
graph[from].push(to);
graph[to].push(from);
});
let result = 0;

const DFS = (start) => {
let stack = [...start];
while (stack.length) {
let node = stack.pop();
if (visited[node]) continue;
visited[node] = true;
stack.push(...graph[node]);
}
};

for (let i = 1; i < graph.length; i++) {
if (!visited[i]) {
result++;
DFS(graph[i]);
}
}

console.log(result);
45 changes: 45 additions & 0 deletions haejeong/week17/유기농배추.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const num = Number(input.shift());
const ds = [
Copy link
Contributor

Choose a reason for hiding this comment

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

방향을 변수 하나로 선언하는 방법도 좋네요

[-1, 0],
[1, 0],
[0, 1],
[0, -1],
];
function bfs(startX, startY) {
const queue = [[startX, startY]];
// queue가 비워지면 탈출
while (queue.length) {
const [x, y] = queue.shift();
// 인접한 1들 다 0으로 만들기
if (!map[x][y]) continue;
else map[x][y] = 0;

// 상하좌우 탐색해 1이 있다면 queue에 push
for (let i = 0; i < 4; i++) {
const xPos = x + ds[i][0];
const yPos = y + ds[i][1];

if (xPos < 0 || yPos < 0 || xPos >= M || yPos >= N) continue;
if (map[xPos][yPos]) queue.push([xPos, yPos]);
}
}
}
for (let i = 0; i < num; i++) {
let worm = 0;
var [M, N, K] = input.shift().split(" ").map(Number);
Copy link
Contributor

Choose a reason for hiding this comment

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

첫 번째 요소를 없애기 위해 shift()를 써도 되는군요!

var map = Array.from(Array(M), () => new Array(N).fill(0));
for (let j = 0; j < K; j++) {
let xy = input.shift().split(" ");
map[xy[0]][xy[1]] = 1;
}
for (let k = 0; k < M; k++) {
for (let l = 0; l < N; l++) {
if (map[k][l]) {
bfs(k, l);
worm++;
}
}
}
console.log(worm);
}
48 changes: 48 additions & 0 deletions haejeong/week18/1012.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 1012: 유기농 배추
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const num = Number(input.shift());
const ds = [
[-1, 0],
[1, 0],
[0, 1],
[0, -1],
];

const bfs = (startX, startY) => {
const queue = [[startX, startY]];
// queue가 빈배열이면 탈출
while (queue.length) {
const [x, y] = queue.shift();
// 인접한 1들 다 0으로 만들기
if (!map[x][y]) continue;
else map[x][y] = 0;

// 상하좌우 탐색해 1이 있다면 queue에 push
for (let i = 0; i < 4; i++) {
const xPos = x + ds[i][0];
const yPos = y + ds[i][1];

if (xPos < 0 || yPos < 0 || xPos >= M || yPos >= N) continue;
if (map[xPos][yPos]) queue.push([xPos, yPos]);
}
}
};

for (let i = 0; i < num; i++) {
let worm = 0;
var [M, N, K] = input.shift().split(" ").map(Number);
var map = Array.from(Array(M), () => new Array(N).fill(0));
for (let j = 0; j < K; j++) {
let xy = input.shift().split(" ");
map[xy[0]][xy[1]] = 1;
}
for (let k = 0; k < M; k++) {
for (let l = 0; l < N; l++) {
if (map[k][l]) {
bfs(k, l);
worm++;
}
}
}
console.log(worm);
}
45 changes: 45 additions & 0 deletions haejeong/week18/1260.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 1260: DFS와 BFS
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const [N, M, V] = input.shift().split(" ").map(Number);
const edges = input.map((v) => v.split(" ").map(Number));
const graph = [...Array(N + 1)].map(() => []);
edges.forEach(([from, to]) => {
graph[from].push(to);
graph[to].push(from);
});

const dfs = (start) => {
const stack = [start];
Copy link
Contributor

Choose a reason for hiding this comment

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

덕분에 스택으로 구현하는 방법도 있다는거 알았네요!

const visited = Array(N + 1).fill(false);
const order = [];
while (stack.length) {
const node = stack.pop();
if (!visited[node]) {
visited[node] = true;
order.push(node);
stack.push(...graph[node]);
}
}
return order.join(" ");
};

const bfs = (start) => {
const queue = [start];
const visited = Array(N + 1).fill(false);
const order = [];
while (queue.length) {
const node = queue.shift();
if (!visited[node]) {
visited[node] = true;
order.push(node);
queue.push(...graph[node]);
}
}
return order.join(" ");
};

graph.forEach((v) => v.sort((a, b) => b - a));
console.log(dfs(V));

graph.forEach((v) => v.sort((a, b) => a - b));
console.log(bfs(V));
30 changes: 30 additions & 0 deletions haejeong/week18/2178.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 2178: 미로 탐색
const input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");
const [yMax, xMax] = input.shift().split(" ");
const map = input.map((v) => v.split("").map((x) => +x));

const stack = [[0, 0, 1]];
const dir = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];

while (stack.length) {
const [x, y, dis] = stack.shift();

for (let i = 0; i < 4; i++) {
const xPos = x + dir[i][0];
const yPos = y + dir[i][1];

if (0 <= xPos && yPos > -1 && xPos < xMax && yPos < yMax) {
if (map[yPos][xPos] === 1) {
map[yPos][xPos] = dis + 1;
stack.push([xPos, yPos, dis + 1]);
}
}
}
}

console.log(map[yMax - 1][xMax - 1]);
33 changes: 33 additions & 0 deletions haejeong/week18/2644.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 2644: 촌수 계산
let fs = require("fs");
let input = fs.readFileSync("/dev/stdin").toString().split("\n");

const n = Number(input[0]);
const [p, q] = input[1].split(" ").map(Number);
const m = Number(input[2]);
const graph = [...Array(n + 1)].map((e) => []);

for (let i = 3; i < m + 3; i++) {
let [parent, child] = input[i].split(" ").map(Number);
graph[parent].push(child);
graph[child].push(parent);
}

const bfs = (graph, startNode, targetNode) => {
const visited = [];
let needVisit = [[startNode, 0]];

while (needVisit.length !== 0) {
const [node, cnt] = needVisit.shift();
if (node === targetNode) return cnt;
if (!visited.includes(node)) {
visited.push(node);
let nodes = graph[node].map((e) => [e, cnt + 1]);
needVisit = [...needVisit, ...nodes];
}
}

return -1;
};

console.log(bfs(graph, p, q));
Loading