[LC] 256. Paint House

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Note:
All costs are positive integers.

Example:

Input: [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Explanation: Paint house 0 into blue, paint house 1 into green, paint house 2 into blue.
             Minimum cost: 2 + 5 + 3 = 10.
class Solution {
    public int minCost(int[][] costs) {
        if (costs == null || costs.length == 0 || costs[0].length == 0) {
            return 0;
        }
        int curRed = costs[0][0];
        int curBlue = costs[0][1];
        int curGreen = costs[0][2];
        int prevRed = curRed;
        int prevBlue = curBlue;
        int prevGreen = curGreen;
        for (int i = 1; i < costs.length; i++) {
            curRed = Math.min(prevBlue, prevGreen) + costs[i][0];
            curBlue = Math.min(prevRed, prevGreen) + costs[i][1];
            curGreen = Math.min(prevRed, prevBlue) + costs[i][2];
            prevRed = curRed;
            prevBlue = curBlue;
            prevGreen = curGreen;
        }
        return Math.min(curRed, Math.min(curBlue, curGreen));
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12053778.html

时间: 2024-08-29 07:41:13

[LC] 256. Paint House的相关文章

256.Paint House

/* * 256.Paint House * 2016-6-21 by Mingyang * 典型的dp,我一开始就想到了说要是用一个二维的dp,dp的另外一个值来表示颜色 * 这个地方最高级的部分就在于!我是在更新cost的matrix,并没有专门来建立一个dp array非常的高明! */ public int minCost(int[][] costs) { //请把最简单的case记住了 if(costs != null && costs.length == 0) return 0

[LeetCode#256] Paint House

Problem: There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses hav

[LeetCode] 256. Paint House_Easy tag: Dynamic Programming

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the sam

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

ISO 7816-4: Annex A: Transportation of APDU messages by T=0

http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_annex-a.aspx Annex A: Transportation of APDU messages by T=0 A.1 Case 1 A.2 Case 2 Short A.2S.1 Le accepted A.2S.2 Le definitely accepted A.2S.3 Le not accepted, La indicated A.2S.4 SW1-

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷

20200108-20200112

244. Shortest Word Distance II - Medium 245. Shortest Word Distance III - Medium 246. Strobogrammatic Number - Easy 247. Strobogrammatic Number II - Medium method: recursion n =1 [0,1,8] n = 2 [11, 88, 69, 96] n = 3 [101, 111, 181, 808, 818, 888, 609