import java.util.Arrays;
/**
* @author xnl
* @Description:
* @date: 2022/6/25 22:43
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[][] costs = {{17,2,17},{16,16,5},{14,3,19}};
solution.minCost(costs);
System.out.println(solution.ans);
}
/**
* 暴力匹配,不行,有问题
*
* @param costs
* @return
*/
int ans = 0;
public int minCost2(int[][] costs) {
if (costs.length == 0 || costs[0].length == 0){
return 0;
}
int index = getFirstMinValue(costs[0]);
for (int i = 1; i < costs.length; i++){
index = getIndex(costs[i], index);
}
return ans;
}
private int getFirstMinValue(int[] arr){
int asInt = Arrays.stream(arr).min().getAsInt();
ans += asInt;
int res = 0;
for (int i = 0; i < arr.length; i++){
if (arr[i] == asInt){
res = i;
break;
}
}
return res;
}
private int getIndex(int[] arr, int index){
int temp = 0;
if (index == 0){
temp = arr[1] > arr[2] ? 2 : 1;
}
if (index == 1){
temp = arr[0] > arr[2] ? 2 : 0;
}
if (index == 2){
temp = arr[0] > arr[1] ? 1 : 0;
}
ans += arr[temp];
return temp;
}
/**
* 暴力匹配,不行,有问题,hhh,使用模拟
* @param costs
* @return
*/
public int minCost(int[][] costs) {
int red = 0, green = 0, blue = 0;
for (int[] cost : costs) {
int r = Math.min(blue, green) + cost[0];
int g = Math.min(blue, red) + cost[1];
int b = Math.min(red, green) + cost[2];
red = r;
blue = b;
green = g;
}
return Math.min(red, Math.max(green, blue));
}
}