Skip to content
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
32 changes: 32 additions & 0 deletions week31/다리를 지나는 트럭.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function solution(bridge_length, weight, truck_weights) {
let time = 0;
let bridge = []; // 다리를 나타내는 큐 (현재 다리를 건너는 트럭들)
let bridgeWeight = 0; // 현재 다리 위에 있는 트럭들의 총 무게

while (truck_weights.length > 0 || bridge.length > 0) {
time++;

// 다리를 다 건넌 트럭 제거
if (bridge.length > 0 && bridge[0][1] === time) {
bridgeWeight -= bridge[0][0]; // 트럭의 무게를 총 무게에서 제거
bridge.shift(); // 다리 큐에서 트럭 제거
}

// 다음 트럭을 다리에 추가할 수 있는지 확인
if (
truck_weights.length > 0 &&
bridgeWeight + truck_weights[0] <= weight && // 무게 제한 확인
bridge.length < bridge_length // 길이 제한 확인
) {
const truck = truck_weights.shift(); // 대기 트럭 중 첫 번째 트럭 가져오기
bridge.push([truck, time + bridge_length]); // 트럭과 도착 시간을 다리 큐에 추가
bridgeWeight += truck; // 다리 위 총 무게 갱신
}
}

return time;
}

console.log(solution(2, 10, [7, 4, 5, 6])); //8
console.log(solution(100, 100, [10])); //101
console.log(solution(100, 100, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])); //110
30 changes: 30 additions & 0 deletions week31/달리기 경주.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function solution(players, callings) {
// 현재 순위를 저장하는 맵 생성
const positionMap = {};
players.forEach((player, index) => {
positionMap[player] = index; // 각 선수의 현재 순위를 저장
});

// 호출된 선수들을 처리
callings.forEach(calling => {
const currentIndex = positionMap[calling];
if (currentIndex > 0) { // 해당 선수가 1등이 아닌 경우에만 처리
const prevIndex = currentIndex - 1;
const prevPlayer = players[prevIndex];

// 배열에서 선수들의 순서를 교환
players[currentIndex] = prevPlayer;
players[prevIndex] = calling;

// 맵에서 순위 정보를 업데이트
positionMap[calling] = prevIndex;
positionMap[prevPlayer] = currentIndex;
}
});

// 최종 순위 배열 반환
return players;
}

console.log(solution(["mumu", "soe", "poe", "kai", "lee"], ["soe", "kai", "kai", "mumu"]));
//["mumu", "kai", "soe", "poe", "lee"]
37 changes: 37 additions & 0 deletions week31/배달.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function solution(N, road, K) {
// 그래프를 인접 리스트로 생성
const graph = Array.from({ length: N + 1 }, () => []);
road.forEach(([a, b, c]) => {
graph[a].push([b, c]); // a -> b로 가는 비용 c를 추가
graph[b].push([a, c]); // b -> a로 가는 비용 c를 추가 (양방향 도로)
});

// 다익스트라 알고리즘
const distances = Array(N + 1).fill(Infinity); // 각 마을까지의 최단 시간을 저장
distances[1] = 0; // 시작 마을은 1번 마을
const pq = [[1, 0]]; // 우선순위 큐: [현재 마을, 현재 거리]

while (pq.length > 0) {
pq.sort((a, b) => a[1] - b[1]); // 거리 기준으로 정렬 (최소 힙처럼 동작)
const [currentNode, currentDistance] = pq.shift();

// 이미 더 짧은 경로를 찾았다면 건너뛰기
if (currentDistance > distances[currentNode]) continue;

// 이웃 마을 탐색
for (const [nextNode, travelTime] of graph[currentNode]) {
const newDistance = currentDistance + travelTime;

if (newDistance < distances[nextNode]) {
distances[nextNode] = newDistance; // 최단 거리 갱신
pq.push([nextNode, newDistance]); // 큐에 추가
}
}
}

// K 이하의 시간으로 도달 가능한 마을 수 계산
return distances.filter(time => time <= K).length;
}

console.log(solution(5, [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]], 3)); //4
console.log(solution(6, [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]], 4)); //4