【剑指 の 精选】热门状态机 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 原题链接和其他优选题解。

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

相关推荐
灰灰老师5 分钟前
数据分析系列--[11] RapidMiner,K-Means聚类分析(含数据集)
人工智能·算法·机器学习·数据挖掘·数据分析·kmeans·rapidminer
追求源于热爱!33 分钟前
记4(可训练对象+自动求导机制+波士顿房价回归预测
图像处理·人工智能·算法·机器学习·回归
taopi202435 分钟前
android java系统弹窗的基础模板
android·java·开发语言
松仔log1 小时前
Java多线程——对象的组合
java·开发语言·jvm
qq_433618441 小时前
哈夫曼树
数据结构·算法
余辉zmh1 小时前
【贪心算法篇】:“贪心”之旅--算法练习题中的智慧与策略(二)
c++·算法·leetcode·贪心算法
酷爱码1 小时前
springboot 动态配置定时任务
java·spring boot·后端
余辉zmh1 小时前
【贪心算法篇】:“贪心”之旅--算法练习题中的智慧与策略(一)
c++·算法·leetcode·贪心算法
计算机-秋大田2 小时前
基于SpringBoot的美食烹饪互动平台的设计与实现(源码+SQL脚本+LW+部署讲解等)
vue.js·spring boot·后端·课程设计·美食
taoyong0012 小时前
代码随想录算法训练营第三十七天-动态规划-完全背包-377. 组合总和 Ⅳ
c++·算法·leetcode·动态规划