LintCode:M-房屋染色

58 篇文章 0 订阅
28 篇文章 0 订阅

LintCode链接

这里有n个房子在一列直线上,现在我们需要给房屋染色,分别有红色蓝色和绿色。每个房屋染不同的颜色费用也不同,你需要设计一种染色方案使得相邻的房屋颜色不同,并且费用最小。

费用通过一个nx3 的矩阵给出,比如cost[0][0]表示房屋0染红色的费用,cost[1][2]表示房屋1染绿色的费用。

 注意事项

所有费用都是正整数

样例

costs = [[14,2,11],[11,14,5],[14,3,10]] return 10

房屋 0 蓝色, 房屋 1 绿色, 房屋 2 蓝色, 2 + 5 + 3 = 10


public class Solution {
    /**
     * @param costs n x 3 cost matrix
     * @return an integer, the minimum cost to paint all houses
     */
    public int minCost(int[][] costs) {
        int n = costs.length;
        if(n==0) return 0;
        //dp,0~i染色完后的最小染色支出
        int[] dp = new int[3];
        dp = costs[0];
        for(int i=1; i<n; i++){
            int[] tmp = new int[3];
            tmp[0] = Math.min(dp[1], dp[2])+costs[i][0];
            tmp[1] = Math.min(dp[0], dp[2])+costs[i][1];
            tmp[2] = Math.min(dp[1], dp[0])+costs[i][2];
            dp = tmp;
        }
        
        return Math.min(dp[0], Math.min(dp[1], dp[2]));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值