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

week_02 5문제 성공 2문제 실패 #4

Open
wants to merge 19 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
45 changes: 45 additions & 0 deletions hanbin/week_02/CODETREE_배열 회전.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const input = require('fs').readFileSync(0).toString().trim().split('\n');

// 반례 못찾음

const [n, m, k] = input[0].split(' ').map(Number);
let arr = input.slice(1).map((it) => it.split(' ').map(Number));

function solution(){
for (let i = 0; i < k; i++){
arr = rotate(arr,n,m);
}

console.log(arr.map((it) => it.join(' ')).join('\n'));
}


function rotate(arr, n, m){
const tmp = arr.map((it) => [...it]);

for (let r = 0, c = 0, i = 1; r < Math.ceil(n/2) && c < Math.ceil(m/2); r++, c++, i++){
while(c < m - i){
tmp[r][c] = arr[r][c+1];
c++;
}

while(r < n - i){
tmp[r][c] = arr[r+1][c];
r++;
}

while(c >= i){
tmp[r][c] = arr[r][c-1];
c--;
}

while(r >= i){
tmp[r][c] = arr[r-1][c];
r--;
}
}

return tmp;
}

solution();
80 changes: 80 additions & 0 deletions hanbin/week_02/CODETREE_숫자 크기 비교.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const input = require('fs').readFileSync(0).toString().trim().split('\n');

// 사전순 정렬 실패

class Queue {
constructor(){
this.storage = {};
this.front = -1;
this.rear = -1;
}

push(value){
this.storage[++this.rear] = value;
}

poll(){
if (this.isEmpty()) {
return null;
}

const value = this.storage[++this.front];
delete this.storage[this.front - 1];
return value;
}

isEmpty(){
return this.front === this.rear;
}
}

function solution(){
const [N, M] = input[0].split(' ').map(Number);
const nodes = Array(N+1).fill(0).map(() => []);
const degrees = Array(N+1).fill(0);

for (let i = 1; i < input.length; i++){
const cur = input[i].split(' ').map(Number);
nodes[cur[0]].push(cur[1]);
degrees[cur[1]]++;
}

let startPoint = new Queue();

for (let i = 1; i < degrees.length; i++){
if (degrees[i] === 0){
startPoint.push([i, 0]);
}
}

console.log(sort(nodes, degrees, startPoint));
}

function sort(nodes, degrees, queue){
let order = [];

while(!queue.isEmpty()){
const cur = queue.poll();

for (let node of nodes[cur[0]]){
degrees[node]--;

if (degrees[node] === 0){
queue.push([node, cur[1] + 1]);
}
}

order.push(cur);
}

order.sort((a, b) => {
if (a[1] === b[1]){
return a[0] - b[0]
}
return a[1] - b[1];
})

return order.map((it) => it[0]).join(' ');
}

solution();
24 changes: 24 additions & 0 deletions hanbin/week_02/CODETREE_정수 3개 고르기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const input = require('fs').readFileSync(0).toString().trim().split('\n');

const [n, m] = input[0].split(' ').map(Number);
const arr = input[1].split(' ').map(Number).sort((a,b) => a - b);

let answer = 0;

for (let i = 0; i < arr.length; i++){
for (let left = 0; left < arr.length-1; left++){
if (left === i) continue;

for (right = left + 1; right < arr.length; right++){
if (right === i) continue;

let sum = arr[i] + arr[left] + arr[right];

if (sum <= m && m - sum < m - answer){
answer = sum;
}
}
}
}

console.log(answer !== 0 ? answer : -1);
57 changes: 57 additions & 0 deletions hanbin/week_02/CODETREE_트리의 지름 길이.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const input = require('fs').readFileSync(0).toString().trim().split('\n');

class Edge {
constructor(to, dist){
this.to = to;
this.dist = dist;
}
}

function solution(){
const N = Number(input[0]);
const edges = input.slice(1).map((it) => it.split(' ').map(Number));
const nodes = Array(N+1).fill(0).map((it) => []);

for (let edge of edges){
nodes[edge[0]].push(new Edge(edge[1], edge[2]));
nodes[edge[1]].push(new Edge(edge[0], edge[2]));
}

const visited = Array(N+1).fill(-1);

DFS(visited, nodes, 1);

let start = getMaxValue(visited);
visited.fill(-1);

DFS(visited, nodes, start[1]);

console.log(getMaxValue(visited)[0]);
}

function DFS(visited, nodes, cur){
if (visited[cur] === -1){
visited[cur]++;
}

for (let edge of nodes[cur]){
if(visited[edge.to] !== -1) continue;

visited[edge.to] = visited[cur] + edge.dist;
DFS(visited, nodes, edge.to);
}
}

function getMaxValue(visited){
let max = 0;
let maxIdx = 0;
for (let i = 1; i < visited.length; i++){
if (max < visited[i]){
max = visited[i];
maxIdx = i;
}
}
return [max,maxIdx];
}

solution();
64 changes: 64 additions & 0 deletions hanbin/week_02/PROG_118667_두 큐 합 같게 만들기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function solution(queue1, queue2) {
const MAX_VALUE = 600_001;
var answer = MAX_VALUE;

const totalQueue = queue1.concat(queue2);

let { left, right } = calBoundary(queue1);
let { queue1Sum, queue2Sum } = init(left, right, totalQueue);

if (
(queue1Sum + queue2Sum) / 2 !==
Math.round((queue1Sum + queue2Sum) / 2)
) {
return -1;
}

let depth = 0;
while (
depth < MAX_VALUE
) {
if (queue1Sum < queue2Sum) {
queue1Sum += totalQueue[(right + 1) % totalQueue.length];
queue2Sum -= totalQueue[(right + 1) % totalQueue.length];
right = (right + 1) % totalQueue.length;
} else if (queue1Sum > queue2Sum){
queue1Sum -= totalQueue[left];
queue2Sum += totalQueue[left];
left = (left + 1) % totalQueue.length;
} else {
break;
}
depth++;
}

return depth === MAX_VALUE ? -1 : depth;
}

function calBoundary(queue1) {
let left = 0;
let right = queue1.length - 1;

return {
left,
right,
};
}

function init(left, right, totalQueue) {
let queue1Sum = 0,
queue2Sum = 0;

for (let i = 0; i < totalQueue.length; i++) {
if (left <= i && i <= right) {
queue1Sum += totalQueue[i];
continue;
}
queue2Sum += totalQueue[i];
}

return {
queue1Sum,
queue2Sum,
};
}
35 changes: 35 additions & 0 deletions hanbin/week_02/PROG_43163_단어 변환.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function solution(begin, target, words) {
const used = Array(words.length).fill(false);

var answer = dfs(0, used, words, begin, target);

return answer === 51 ? 0 : answer;
}

function dfs(depth, used, words, curWord, targetWord){
if (curWord === targetWord) return depth;

let res = 51;

for (let i = 0; i < words.length; i++){
if (used[i]) continue;

if (diffCheck(curWord, words[i]) === 1){
used[i] = true;
res = Math.min(res, dfs(depth + 1, used, words, words[i], targetWord));
used[i] = false;
}
}

return res;
}

function diffCheck(word1, word2){
let cnt = 0;

for (let i = 0; i < word1.length; i++){
cnt += word1[i] !== word2[i] ? 1 : 0;
}

return cnt;
}
65 changes: 65 additions & 0 deletions hanbin/week_02/PROG_92341_주차 요금 계산.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
function solution(fees, records) {
var answer = [];

const record = new Map();
const result = new Map();

for (data of records){
let [inputTime, carNumber, info] = data.split(' ');
const parsedTime = parseTime(inputTime);
const calTime = handleRecord(parsedTime, carNumber, record);

if (calTime === -1) continue;

const prevData = result.get(carNumber) ?? 0;
result.set(carNumber, prevData + calTime);
}

const calculatedFee = new Map();

for (let [carNumber, time] of record){
const LIMIT_TIME = 60 * 24 - 1;

let curTime = LIMIT_TIME - record.get(carNumber);

result.set(carNumber, (result.get(carNumber) ?? 0) + curTime);
}

for (let time of result){
calculatedFee.set(time[0], calFee(time, fees, record));
}


const tmp = [...calculatedFee].sort((a, b) => a[0] - b[0]);
for (let it of tmp){
answer.push(it[1]);
}

return answer;
}

function parseTime(time){
const [hour, minute] = time.split(':').map(Number);

return hour * 60 + minute;
}

function handleRecord(time, carNumber, record){
const hasRecord = record.has(carNumber);

if (hasRecord){
const data = record.get(carNumber);
record.delete(carNumber);
return time - data;
}

record.set(carNumber, time);
return -1;
}

function calFee([carNumber, parsedTime], [time, fee, unitTime, unitFee], record){
if (parsedTime <= time) return fee;

return fee + Math.ceil((parsedTime - time) / unitTime) * unitFee;
}

Loading