【剑指 の 精选】热门状态机 DP 运用题

题目描述

这是 LeetCode 上的 剑指 Offer II 091. 粉刷房子 ,难度为 中等

Tag : 「状态机 DP」、「动态规划」

假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。

当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的正整数矩阵 costs 来表示的。

例如,costs[0][0] 表示第 <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 0 </math>0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 1 </math>1 号房子粉刷成绿色的花费,以此类推。

请计算出粉刷完所有房子最少的花费成本。

示例 1:

lua 复制代码
输入: costs = [[17,2,17],[16,16,5],[14,3,19]]

输出: 10

解释: 将 0 号房子粉刷成蓝色,1 号房子粉刷成绿色,2 号房子粉刷成蓝色。
最少花费: 2 + 5 + 3 = 10。

示例 2:

lua 复制代码
输入: costs = [[7,6,2]]

输出: 2

提示:

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> c o s t s . l e n g t h = = n costs.length == n </math>costs.length==n
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> c o s t s [ i ] . l e n g t h = = 3 costs[i].length == 3 </math>costs[i].length==3
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = n < = 100 1 <= n <= 100 </math>1<=n<=100
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = c o s t s [ i ] [ j ] < = 20 1 <= costs[i][j] <= 20 </math>1<=costs[i][j]<=20

状态机 DP

为了方便,我们记 costscs

根据题意,当我们从前往后决策每间房子的颜色时,当前房子所能刷的颜色,取决于上一间房子的颜色。

我们可以定义 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 为考虑下标不超过 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i 的房子,且最后一间房子颜色为 <math xmlns="http://www.w3.org/1998/Math/MathML"> j j </math>j 时的最小成本。

起始我们有 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ 0 ] [ i ] = c s [ 0 ] [ i ] f[0][i] = cs[0][i] </math>f[0][i]=cs[0][i],代表只有第一间房子时,对应成本为第一间房子的上色成本。

然后不失一般性考虑, <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 该如何计算: <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 为所有 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ p r e v ] f[i - 1][prev] </math>f[i−1][prev](其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> p r e v ≠ j prev \neq j </math>prev=j)中的最小值加上 <math xmlns="http://www.w3.org/1998/Math/MathML"> c s [ i ] [ j ] cs[i][j] </math>cs[i][j]。

本质上这是一道「状态机 DP」问题:某些状态只能由规则限定的状态所转移,通常我们可以从 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 能够更新哪些目标状态(后继状态)进行转移,也能够从 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ j ] f[i][j] </math>f[i][j] 依赖哪些前置状态(前驱状态)来转移。

一些细节:考虑到我们 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i ] [ X ] f[i][X] </math>f[i][X] 的计算只依赖于 <math xmlns="http://www.w3.org/1998/Math/MathML"> f [ i − 1 ] [ X ] f[i - 1][X] </math>f[i−1][X],因此我们可以使用三个变量来代替我们的动规数组。

Java 代码:

Java 复制代码
class Solution {
    public int minCost(int[][] cs) {
        int n = cs.length;
        int a = cs[0][0], b = cs[0][1], c = cs[0][2];
        for (int i = 1; i < n; i++) {
            int d = Math.min(b, c) + cs[i][0];
            int e = Math.min(a, c) + cs[i][1];
            int f = Math.min(a, b) + cs[i][2];
            a = d; b = e; c = f;
        }
        return Math.min(a, Math.min(b, c));
    }
}

Java 代码:

Java 复制代码
class Solution {
    public int minCost(int[][] cs) {
        int n = cs.length;
        int a = cs[0][0], b = cs[0][1], c = cs[0][2];
        for (int i = 0; i < n - 1; i++) {
            int d = Math.min(b, c) + cs[i + 1][0];
            int e = Math.min(a, c) + cs[i + 1][1];
            int f = Math.min(a, b) + cs[i + 1][2];
            a = d; b = e; c = f;
        }
        return Math.min(a, Math.min(b, c));
    }
}
  • 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n × C ) O(n \times C) </math>O(n×C),其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> C = 3 C = 3 </math>C=3 为颜色数量
  • 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 1 ) O(1) </math>O(1)

最后

这是我们「刷穿 LeetCode」系列文章的第 剑指 Offer II 091 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:github.com/SharingSour...

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉

相关推荐
周杰伦_Jay42 分钟前
【Java虚拟机(JVM)全面解析】从原理到面试实战、JVM故障处理、类加载、内存区域、垃圾回收
java·jvm
摇滚侠2 小时前
Spring Boot 3零基础教程,IOC容器中组件的注册,笔记08
spring boot·笔记·后端
未来之窗软件服务4 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
程序员小凯4 小时前
Spring Boot测试框架详解
java·spring boot·后端
豐儀麟阁贵4 小时前
基本数据类型
java·算法
_extraordinary_5 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
你的人类朋友5 小时前
什么是断言?
前端·后端·安全
程序员 Harry5 小时前
深度解析:使用ZIP流式读取大型PPTX文件的最佳实践
java
程序员小凯6 小时前
Spring Boot缓存机制详解
spring boot·后端·缓存
wxweven6 小时前
校招面试官揭秘:我们到底在寻找什么样的技术人才?
java·面试·校招