博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode/LeetCode] Paint House I & Paint House II
阅读量:6820 次
发布时间:2019-06-26

本文共 2994 字,大约阅读时间需要 9 分钟。

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 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, costs0 is the cost of painting house 0 with color red; costs1 is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

Example

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

house 0 is blue, house 1 is green, house 2 is blue, 2 + 5 + 3 = 10

Note

在原数组上动规,每一行对应一个房子,每一个元素代表从第一行的房子到这一行的房子选择这一种颜色所花的最小开销。所以每个元素 = 该元素的值 + 上一行两个与该元素不同列元素的值的较小者

动规到

Solution

public class Solution {    public int minCost(int[][] cost) {        if (cost == null || cost.length == 0) return 0;        int n = cost.length - 1;        for (int i = 1; i <= n; i++) {            cost[i][0] += Math.min(cost[i-1][1], cost[i-1][2]);            cost[i][1] += Math.min(cost[i-1][0], cost[i-1][2]);            cost[i][2] += Math.min(cost[i-1][0], cost[i-1][1]);        }        return Math.min(cost[n][0], Math.min(cost[n][1], cost[n][2]));    }}

Paint House II

Problem

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, costs0 is the cost of painting house 0 with color 0; costs1 is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

Example

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

house 0 is color 2, house 1 is color 3, house 2 is color 2, 2 + 5 + 3 = 10

Challenge

Could you solve it in O(nk)?

Note

和Paint House的思路一样,依然是在cost数组上更新最小花销之和。不过这次要记录三个变量:本行最小值,本行第二小值,本行最小值下标。这三个变量每行更新一次,同时也要存储上一次循环的三个变量。这样做的目的是当前一行最小值和之前一行最小值在同一列时,取之前一行的次小值,也就避免了相邻两行取同种颜色的问题。

Solution

public class Solution {    public int minCostII(int[][] cost) {        if (cost == null || cost.length == 0) return 0;        int preMin = 0, preSec = 0, preIndex = -1;        int n = cost.length, k = cost[0].length;        for (int i = 0; i < n; i++) {            int curMin = Integer.MAX_VALUE, curSec = Integer.MAX_VALUE, curIndex = 0;            for (int j = 0; j < k; j++) {                cost[i][j] += preIndex == j ? preSec : preMin;                if (cost[i][j] < curMin) {                    curSec = curMin;                    curMin = cost[i][j];                    curIndex = j;                }                else if (cost[i][j] < curSec) {                    curSec = cost[i][j];                }            }            preMin = curMin;            preSec = curSec;            preIndex = curIndex;        }        return preMin;    }}

转载地址:http://wipzl.baihongyu.com/

你可能感兴趣的文章
你听过的最心酸的一句话是什么?
查看>>
ios 图片处理( 1.按比例缩放 2.指定宽度按比例缩放
查看>>
nginx 直接在配置文章中设置日志分割
查看>>
(算法)二叉树中两个结点的最近公共父结点
查看>>
Android实例-监测网络状态及一些事件(XE8+小米2)
查看>>
HDU 4686 Arc of Dream(递归矩阵加速)
查看>>
Apache 配置 Basic 认证
查看>>
使用 XML 实现 REST 式的 SOA
查看>>
SQL Server 日志收缩
查看>>
High accuracy voltage regulator
查看>>
directory not found for option
查看>>
【转载】菜鸟Ubuntu下安装Android Studio
查看>>
三阶魔方中心块调整配方和记忆方法
查看>>
Android Studio 快捷键整理分享
查看>>
Android Studio安装、配置
查看>>
SAP FI 财务模块 关键用户 考试练习 问卷
查看>>
Unity3D之Mecanim动画系统学习笔记(八):Animator Layers(动画分层)
查看>>
PIC24FJ64GB002 with bluetooth USB dongle
查看>>
C# ZPL II 命令打印标签
查看>>
代码面试之广义表
查看>>