• [leetcode]265. Paint House II粉刷房子(K色可选)


    There are a row of n houses, each house can be painted with one of the k colors. 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 k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

    Note:
    All costs are positive integers.

    Example:

    Input: [[1,5,3],[2,9,4]]
    Output: 5
    Explanation: Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5; 
                 Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5. 

    题意:

    一排有n套房子,每个房子可以选一种颜色(K种不同颜色)来粉刷。由于每个房子涂某种颜色的花费不同,求相邻房子不同色的粉刷最小花费。

    做这个题的时候,想起跟老妈一起去过的Santa Cruz海棠花节的时候去过的这个网红海滩。

    所以这个题,对于景区想打造网红般的colorful houses来讲,还是蛮有显示意义。

    Solution1: DP

     

    code

     1 class Solution {
     2     public int minCostII(int[][] costs) {
     3         // sanity check
     4         if (costs == null || costs.length == 0) return 0;
     5         
     6         //init NO.1 house's curMin1, curMin2
     7         int curMin1 = -1; // most minimum
     8         int curMin2 = -1; // second minimum
     9         for (int j = 0; j < costs[0].length; j++) {
    10             if (curMin1 < 0 || costs[0][j] < costs[0][curMin1]) {
    11                 curMin2 = curMin1;
    12                 curMin1 = j;
    13             } else if (curMin2 < 0 || costs[0][j] < costs[0][curMin2]) {
    14                 curMin2 = j;
    15             }
    16         }
    17         // scan from NO.2 house 
    18         for (int i = 1; i < costs.length; i++) {  // scan n houses
    19             // get NO.1 house's curMin1, curMin2
    20             int preMin1 = curMin1;
    21             int preMin2 = curMin2;
    22             // init NO.2 house's curMin1, curMin2
    23             curMin1 = -1;
    24             curMin2 = -1;
    25             // try k colors
    26             for (int j = 0; j < costs[0].length; j++) {
    27                 if (j != preMin1) {  
    28                     costs[i][j] += costs[i - 1][preMin1];
    29                 } else {
    30                     costs[i][j] += costs[i - 1][preMin2];
    31                 }
    32                 // update NO.2 house's curMin1, curMin2
    33                 if (curMin1 < 0 || costs[i][j] < costs[i][curMin1]) {
    34                     curMin2 = curMin1;
    35                     curMin1 = j;
    36                 } else if (curMin2 < 0 || costs[i][j] < costs[i][curMin2]) {
    37                     curMin2 = j;
    38                 }
    39             }
    40         }
    41         return costs[costs.length - 1][curMin1];
    42     }
    43 }
  • 相关阅读:
    简单的排列问题的总结
    php学习
    入门shader,并实现normal mapping on earth
    github 操作查阅 创建仓库 代码更新
    Debian WSL desktop install on windows 10
    使用python三元运算if else
    搜索引擎语法整理
    个人小程序学习笔记,会持续学习并更新!
    Windows Form父子两个窗体之间的传值测试
    Asp.netCore 3.0 Web 实现Oauth2.0微信授权登陆的测试
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9108862.html
Copyright © 2020-2023  润新知