-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaint_house_with_n_colors.java
63 lines (53 loc) · 1.43 KB
/
paint_house_with_n_colors.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[][] = new int[n][k];
int dp[][] = new int[n][k];
for(int i = 0 ;i < n ; i++)
{
for(int j = 0 ; j < k ; j++)
{
arr[i][j] = sc.nextInt();
if(i == 0)
{
dp[0][j] = arr[0][j];
}
}
}
for(int i = 1 ;i < n ; i++)
{
for(int j = 0 ; j < k ; j++)
{
dp[i][j] = arr[i][j] + minimum(dp,i-1,j) ;
}
}
int ann = Integer.MAX_VALUE ;
for(int i = 0 ; i < k ; i++)
{
if(ann > dp[n-1][i])
{
ann = dp[n-1][i];
}
}
System.out.println(ann);
}
public static int minimum(int[][]dp , int row , int exclude)
{
int min = Integer.MAX_VALUE ;
for(int i = 0 ; i < dp[row].length ; i++)
{
if(i!=exclude)
{
if(min > dp[row][i])
{
min = dp[row][i];
}
}
}
return min ;
}
}