栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

剑指 Offer II 091. 粉刷房子

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

剑指 Offer II 091. 粉刷房子


import java.util.Arrays;


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);
    }

    
    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;
    }

    
    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));
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/992828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号