-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaint House.java
46 lines (31 loc) · 1.46 KB
/
Paint House.java
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
// 1. You are given a number n, representing the number of houses.
// 2. In the next n rows, you are given 3 space separated numbers representing the cost of painting nth house with red or blue or green color.
// 3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[][] arr = new int[n][3];
for (int i = 0; i < n; i++) {
String str = br.readLine();
String[] items = str.split(" ");
arr[i][0] = Integer.parseInt(items[0]);
arr[i][1] = Integer.parseInt(items[1]);
arr[i][2] = Integer.parseInt(items[2]);
}
long red = arr[0][0];
long blue = arr[0][1];
long green = arr[0][2];
for (int i = 1; i < arr.length; i++) {
long nred = arr[i][0] + Math.min(blue, green);
long nblue = arr[i][1] + Math.min(red, green);
long ngreen = arr[i][2] + Math.min(red, blue);
red = nred;
blue = nblue;
green = ngreen;
}
System.out.println(Math.min(red, Math.min(blue, green)));
}
}