Skip to content
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

김현정 / 17주차 #25

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8189665
[BOJ] 예산 / 실버3 / 1시간
sena-22 Mar 28, 2023
20ee6e0
[BOJ] 나무자르기 / 실버2 / ?
sena-22 Mar 29, 2023
6d1d911
[BOJ] 용돈관리 / 실버2 / 1시간
sena-22 Mar 29, 2023
f44974a
[BOJ] 연결요소의 개수 / 실버2
sena-22 Apr 5, 2023
285637c
[BOJ] 유기농 배추 / 실버2
sena-22 Apr 5, 2023
30bceb2
[BOJ] 경로 찾기 / 실버1
sena-22 Apr 5, 2023
133b1c7
[BOJ] 안전영역 / 실버1
sena-22 Apr 6, 2023
810b2a2
[BOJ] DOM / 실버2
sena-22 Apr 6, 2023
841fe3e
[BOJ] DOM / 실버 2
sena-22 Apr 6, 2023
e3d47a2
[BOJ] 미로 탐색 / 실버1 / 1시간
sena-22 Apr 12, 2023
22c00da
[BOJ] 유기농 배추 / 실버2 / 40분
sena-22 Apr 12, 2023
c91ed11
[BOJ] DFS와 BFS / 실버2 / Error
sena-22 Apr 12, 2023
a0e615e
[BOJ] 돌게임/ 실버5 / 40분
sena-22 Apr 21, 2023
351fa85
[BOJ] 1로 만들기 / 실버3 / 40분
sena-22 Apr 21, 2023
3a76470
[BOJ] 이친수 / 실버3 / 80분
sena-22 Apr 21, 2023
e1dd991
[BOJ] 촌수계산 / 실버2 / 1시간
sena-22 Apr 27, 2023
603462e
[BOJ] 01타일 / 실버3 / 40분
sena-22 May 2, 2023
95ecb29
[BOJ] 2xn 타일링 / 실버 3 / 20분
sena-22 May 2, 2023
6e7d52b
[BOJ] 사탕게임 / 실버3 / 80분
sena-22 May 10, 2023
fc58bce
[BOJ] 부분수열의 합 / 실버2 / 40분
sena-22 May 11, 2023
fbea347
[BOJ] 연속합 / 실버 2 / 1시간
sena-22 May 24, 2023
0bc9785
[BOJ] 진우의 달 여행 / 실버4 / 2시간?
sena-22 May 24, 2023
a1dad2a
[BOJ] 부분합 / 골드4 / 1시간
sena-22 May 31, 2023
6ecfd55
[BOJ] 좋다 / 골드4 / 1시간
sena-22 May 31, 2023
9709d2d
[BOJ] 숫자야구 / 실버3 / ?
sena-22 May 31, 2023
cf60447
[BOJ] List of Unique Numbers / 골드5 / 시간 초과
sena-22 May 31, 2023
0e2cb5f
[BOJ] List of Unique Numbers / 골드5 / ?
sena-22 May 31, 2023
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
34 changes: 34 additions & 0 deletions hyunjung/week16/나무자르기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")

const [n, m] = input[0].split(" ").map(Number)
const arr = input[1].split(" ").map(Number)

function solution(n, m, trees) {
arr.sort((a, b) => a - b)
let height = 0
start = 0
end = Math.max(...trees)

for (let i = 0; i < n; i++) {
while (start <= end) {
let mid = Math.floor((start + end) / 2)
let woodSum = 0
trees.forEach((tree) => {
let wood = tree - mid
if (wood > 0) woodSum += wood
})
if (woodSum >= m) {
if (mid > height) height = mid
start = mid + 1
} else {
end = mid - 1
}
}
}
return height
}
//제출
const answer = solution(n, m, arr)
console.log(answer)
31 changes: 31 additions & 0 deletions hyunjung/week16/예산.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")

let [n, arr, budget] = [+input[0], input[1].split(" ").map(Number), +input[2]]

const solution = (n, arr, budget) => {
arr = arr.sort((a, b) => a - b)

let start = 0
let end = arr.at(-1)

while (start <= end) {
let mid = parseInt((start + end) / 2)
let sum = 0
for (let i = 0; i < n; i++) {
if (arr[i] > mid) {
//요청금액 > 상한액
sum += mid //상한액 더하기
} else {
//요청금액 < 상한액
sum += arr[i] //요청금액 더하기
}
}
sum <= budget ? (start = mid + 1) : (end = mid - 1) //총액이 예산보다 작으면 최소금액 증가, 크면 최소금액 감소
}
return end
}

const answer = solution(n, arr, budget)
console.log(answer)
54 changes: 54 additions & 0 deletions hyunjung/week16/용돈관리.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")

let [n, m] = input[0].split(" ")
const arr = input.slice(1).map(Number)

n = +n
m = +m

const solution = (days, withdrawal, A) => {
let start = Math.min(...A)
let end = A.reduce((a, c) => a + c, 0)
let K = Infinity

while (start <= end) {
let min = 0
let mid = Math.floor((start + end) / 2)
let sum = mid
let cnt = 0

for (let i = 0; i < days; i++) {
if (sum - A[i] >= 0) {
sum -= A[i]
} else {
cnt++
sum = mid - A[i]
if (sum < 0) {
min = A[i]
break
}
}
}
if (min > 0) {
start = min
continue
}

if (sum < mid) cnt++
if (cnt <= withdrawal) {
if (K > mid) {
K = mid
}
end = mid - 1
} else {
start = mid + 1
}
}
return K
}

// 제출
const answer = solution(n, m, arr)
console.log(answer)
42 changes: 42 additions & 0 deletions hyunjung/week17/DOM.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//시간 초과
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")
const [n, m, p] = input[0].split(" ").map(Number)
const channels = input.slice(1).map((el) => el.split(" ").map(Number))

const solution = (n, m, p, channels) => {
let curChannel = p
let cnt = 0
let searched = new Array(m + 1).fill(false)

const changeChannel = (channels, curChannel) => {
if (searched[curChannel]) {
return (cnt = -1)
}
searched[curChannel] = true

for (let i = 0; i < channels.length; i++) {
if (channels[i][1] === curChannel) {
//현재 채널이 싫어하는 채널이면
curChannel = channels[i][0] //좋아하는 채널로 변경

if (searched[curChannel]) {
// 변경한 채널이 검색한 채널이면 리턴
return (cnt = -1)
} else {
cnt++
return changeChannel(channels, curChannel)
}
}
}
}

changeChannel(channels, curChannel)

return cnt
}

//제출
const answer = solution(n, m, p, channels)
console.log(answer)
37 changes: 37 additions & 0 deletions hyunjung/week17/DOM_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")
const [n, m, p] = input[0].split(" ").map(Number)
const channels = input.slice(1).map((el) => el.split(" ").map(Number))

const solution = (n, m, first, channels) => {
let A = []
let cnt = 0

let visited = [...Array(m + 1)].fill(false)
for (let i = 1; i <= m; i++) {
A[i] = []
}

for (let i = 0; i < n; i++) {
let [like, hate] = channels[i]
if (A[hate].length === 0) A[hate] = like
}

const DFS = (node) => {
if (visited[node]) return -1
visited[node] = true
for (let i = 1; i <= m; i++) {
if (A[node] === i) {
cnt++
return DFS(i)
}
}
}

if (DFS(first) === -1) return -1
return cnt
}

const answer = solution(n, m, p, channels)
console.log(answer)
29 changes: 29 additions & 0 deletions hyunjung/week17/경로찾기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")
let n = input[0]
n = +n
const graph = input.slice(1).map((el) => el.split(" ").map(Number))

const solution = (n, graph) => {
const result = [...Array(n)].map(() => Array(n).fill(0)) // 답을 저장할 2차원 배열
Copy link
Contributor

Choose a reason for hiding this comment

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

이런식으로 2차원 배열을 생성할 수 있군요...!


const DFS = (edge, line, visited) => {
for (let i = 0; i < n; i++) {
if (graph[edge][i] && !visited[i]) {
visited[i] = true
result[line][i] = 1
DFS(i, line, visited) //line은 계속 고정
}
}
}

for (let i = 0; i < n; i++) {
const visited = new Array(n).fill(false)
DFS(i, i, visited)
}
return result.map((v) => v.join(" ")).join("\n")
}

const answer = solution(n, graph)
console.log(answer)
48 changes: 48 additions & 0 deletions hyunjung/week17/안전영역.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")
let n = input[0]
n = +n
const map = input.slice(1).map((el) => el.split(" ").map(Number))

const solution = (n, area) => {
let max = 0

const DFS = (row, col, h, visited) => {
let dx = [0, 1, 0, -1]
let dy = [1, 0, -1, 0]

for (let i = 0; i < 4; i++) {
let ax = row + dx[i],
ay = col + dy[i]
if (ax >= 0 && ay >= 0 && ax < n && ay < n) {
if (!visited[ax][ay]) {
visited[ax][ay] = true
DFS(ax, ay, h, visited)
}
}
}
}

for (let h = 0; h <= 100; h++) {
let cnt = 0
const visited = [...Array(n)].map((_, x) =>
[...Array(n)].map((_, y) => area[x][y] <= h)
)
for (let row = 0; row < n; row++) {
for (let col = 0; col < n; col++) {
if (!visited[row][col]) {
visited[row][col] = true
DFS(row, col, h, visited)
cnt++
}
}
}
max = Math.max(max, cnt)
}

return max
}

const answer = solution(n, map)
console.log(answer)
46 changes: 46 additions & 0 deletions hyunjung/week17/연결요소의개수.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require("fs")
const readFileSyncAddress = "/dev/stdin"
const input = fs.readFileSync(readFileSyncAddress).toString().trim().split("\n")
const [n, m] = input[0].split(" ").map(Number)

const solution = (n, m, input) => {
let A = []
let cnt = 0
let visited = new Array(n + 1).fill(false)

for (let i = 1; i < n + 1; i++) {
A[i] = []
}

//A 인접 리스트에 그래프 데이터 저장하기
for (let i = 0; i < m; i++) {
let [from, to] = input[i + 1].split(" ").map(Number)
A[from].push(to)
A[to].push(from)
}

const DFS = (start) => {
if (visited[start]) return
visited[start] = true //방문처리

for (let i = 0; i < A[start].length; i++) {
const next = A[start][i]
if (!visited[next]) {
DFS(next) //방문하지 않은 노드만 탐색
}
}
}

for (let i = 1; i <= n; i++) {
if (!visited[i]) {
DFS(i) //방문하지 않은 노드가 있으면 dfs실행
cnt++ //연결 요소 개수++
}
}

return cnt
}

//제출
const answer = solution(n, m, input)
console.log(answer)
Loading