Skip to content

Commit

Permalink
Comment: 서울에서 경산까지
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Jul 23, 2024
1 parent 4326226 commit 7b91297
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion baekjoon/14863.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 서울에서 경산까지 : 동적 계획법
// 서울에서 경산까지 : 동적 계획법, 배낭 문제
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
Expand Down Expand Up @@ -39,3 +39,23 @@ for (let i = 1; i < cityCnt; i++) {
}

console.log(dp[cityCnt - 1][maxMinute]);

// 배낭 문제 풀이법
// - 열(최대시간), 행(도시), 값(모금액)
// - 현재 도시를 반드시 거쳐야 하기 때문에 dp[i - 1][j]에서는 가져오지 않음
// - 현재 시간을 채우는 방법: 이전 도시에서(i - 1) 현재 시간이 되게하는 두 가지 방법(j - walkTime, j - rideTime)을 참조

// - 모든 값을 -Infinity로 초기화 : 어떠한 방법을 사용하더라도 도달할 수 없는 시간
// - 각각의 도시에서 최소값을 누적한 값 이전의 모든 열은 -Infinity
// - 최소값 누적한 값 이후에는 값이 존재 => 100분으로 갈 수 있는 곳은 101분이어도 갈 수 있기 때문
// - dp[i - 1][j - walkTime], dp[i - 1][j - rideTime] 둘 중에 하나라도 값이 있으면 의미가 있는 시간(최대 시간 내 이동할 수 있음)
// - 없다면, 최소값을 누적한 값 이전의 열이라고 볼 수 있음
// - e.g.
// 3 10
// 1 1 3 3
// 3 3 5 5
// 5 5 7 7

// -Infinity 1 1 3 3 3 3 3 3 3 3 (1 - 1 = 0)
// -Infinity -Infinity -Infinity -Infinity 4 4 6 6 8 8 8 (1 + 3 - 1 = 3)
// -Infinity -Infinity -Infinity -Infinity -Infinity -Infinity -Infinity -Infinity -Infinity 9 9 (1 + 3 + 5 - 1 = 8)

0 comments on commit 7b91297

Please sign in to comment.