forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDH_214289.java
More file actions
64 lines (58 loc) · 2.57 KB
/
DH_214289.java
File metadata and controls
64 lines (58 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
class DH_214289 {
public static void main(String[] args) {
int temperature = 9;
int t1 = 7;
int t2 = 8;
int a = 10;
int b = 100;
int[] onboard = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
System.out.println(solution(temperature, t1, t2, a, b, onboard));;
}
static final int INF = 1_000_000_000;
public static int solution(int temperature, int t1, int t2, int a, int b, int[] onboard) {
//에어컨 키는 것은 무조건 온도 증가 행위임을 나타내기 위해 temp 재설정
int temp = temperature > t2 ? t1 - (temperature - t2) : temperature;
//외부온도를 0으로 맞춰줌
t1 -= temp;
t2 -= temp;
// dp[a][b]: a분에 b온도를 맞추기 위한 최소 비용
// 0도부터 t2도까지 확인하므로 dp 크기는 '행: onboard.length, 열: t2 + 1'이 됨
int[][] dp = new int[onboard.length][t2 + 2];
for (int i = 0; i < onboard.length; i++) Arrays.fill(dp[i], INF);
dp[0][0] = 0;
for (int i = 1; i < onboard.length; i++) {
//다음 3가지 케이스 중 하나
//1. 온도 올리기 (에어컨 가동 -> 비용 a)
//2. 온도 유지 (에어컨 가동 -> 비룔 b) + (목표온도 = 외부온도 -> 비용 0)
//3. 온도 하강 (에어컨 끄기 -> 비용 0)
for (int j = 0; j <= t2 + 1; j++) {
//탑승중일때 온도 범위 벗어나면 구하지 않음
if (onboard[i] == 1 && (j < t1 || j > t2)) continue;
int min = INF;
//1. 외부온도 == 목표온도
if (j == 0) {
//온도 올리기
//온도 유지하기
min = Math.min(min, dp[i-1][j]);
//온도 내리기
if (j + 1 <= t2 + 1) min = Math.min(min, dp[i-1][j+1]);
}
//2. 외부온도 != 목표온도
else {
//온도 올리기
if (j - 1 >= 0) min = Math.min(min, dp[i-1][j-1] + a);
//온도 유지하기
min = Math.min(min, dp[i-1][j] + b);
//온도 내리기
if (j + 1 <= t2 + 1) min = Math.min(min, dp[i-1][j+1]);
}
dp[i][j] = min;
}
}
int result = Integer.MAX_VALUE;
for (int j = 0; j <= t2 + 1; j++)
result = Math.min(result, dp[onboard.length-1][j]);
return result;
}
}