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;
            for(int i = 1; i < costs.length; i++){
                // 涂第一种颜色的话,上一个房子就不能涂第一种颜色,这样我们要在上一个房子的第二和第三个颜色的最小开销中找最小的那个加上
                costs[i][0] = costs[i][0] + Math.min(costs[i - 1][1], costs[i - 1][2]);
                // 涂第二或者第三种颜色同理
                costs[i][1] = costs[i][1] + Math.min(costs[i - 1][0], costs[i - 1][2]);
                costs[i][2] = costs[i][2] + Math.min(costs[i - 1][0], costs[i - 1][1]);
            }
            // 返回涂三种颜色中开销最小的那个
            return Math.min(costs[costs.length - 1][0], Math.min(costs[costs.length - 1][1], costs[costs.length - 1][2]));
        }
时间: 2024-08-01 06:34:00

256.Paint House的相关文章

[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

[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 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

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

Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)

C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line. Your task is to paint