[leetcode] 276. Paint Fence 解题报告

vecodo-luajava-master_iphone_VEC-466友人_ OSCHINA 的 iPhone 客户端源码,可直接在 App Store上搜索“开源中国”来安装此app 立即下载

 

题目链接:https://leetcode.com/problems/paint-fence/

 

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color.

Return the total number of ways you can paint the fence.

Note:
n and k are non-negative integers.

思路: 题意说的是不能有超过连续两个相同的颜色, 也就是说最多有两个相邻柱子染同样颜色.

这是一个双重dp的问题,对于第i个柱子染什么颜色我们要考虑是否和之前染一样的颜色

1. 如果和i-1染一样的颜色,那么第i-1个柱子就不能和第i-2染一样的颜色

2. 如果和第i-1染不一样的颜色,那么第i个柱子的染色方案数即为 sum(i-1) * (k-1)

也就是为了得到第i个柱子的染色方案,我们需要利用之前的两个状态,一个是i-1和i-2染一样颜色的方案数,另一个是i-1和i-2染不一样颜色的方案数,这样我们需要两个dp数组dp1, dp2分别记录对应的状态

然后考虑两个dp数组的递推公式:

dp1[i] = dp2[i-1],这个表示如果i和i-1染一样的色,那么染色方案为dp2[i-1]

dp2[i] = (dp1[i-1] + dp2[i-1]) * (k-1),表示i和i-1染不一样染色,对于第i-1的每一种染色方案,我们有k-1种颜色可选

初始状态为:

dp1[1] = 0

dp2[1] = k

JAVA 代码如下:

class Solution {
    public int numWays(int n, int k) {
        if(n == 0 || k == 0) return 0;
        int[] dp1 = new int[n+1], dp2 = new int[n+1];
        dp1[1] = 0;
        dp2[1] = k;
        for(int i = 2; i <= n; i++) {
            dp1[i] = dp2[i-1];
            dp2[i] = (dp1[i-1] + dp2[i-1]) * (k-1);
        }
        return dp1[n] + dp2[n];
    }
}

有了上面的基础之后我们还可以考虑状态压缩:

C++代码

class Solution {
public:
    int numWays(int n, int k) {
        if(n==0 || k==0 || (k==1 && n>2)) return 0;
        int same = 0, diff = k, total = k;
        for(int i = 2; i <= n; i++)
        {
            same = diff, diff = (k-1)*total;
            total = same + diff;
        }
        return total;
    }
};

参考: https://leetcode.com/discuss/85147/complete-explanation-o-n-time-o-1-space
 

还有一个进阶的题目:https://leetcode.com/problems/number-of-music-playlists/

思路差不多

 

【keras】走近VGG16卷积神经网络         VGG16是一个16层的卷积神经网络模型,输入数据的维度为224*224*3,即长宽均为224像素的RGB彩色图片,VGG16模型的权重由ImageNet训练而来。由于其具有简单实用的优点,因此,在图像分类和目标检测任务中得到了较为广泛的应用。 1.卷积神经网络的基本概念 1.1卷积         简单的将其理解为一种数学运算,具体的计算过程如下图所示:       ... 阅读详情

相关推荐

一次HTTP完整得请求过程

一次HTTP完整的请求过程 一,大致流程: 根据域名和DNS解析到服务器的IP地址(DNS+CDN) 通过ARP协议获得IP地址对应的物理机器得MAC地址 浏览器对服务器发起三次握手 建立TCP连接后发起HTTP请求报文 服务器响应HTTP请求,将响应报文返回浏览器 短连接情况下,请求结束通过TCP四次挥手关闭连接,长连接在没有访问服务器得若干时间后,进行连接关闭 浏览器得到响应信...

qq_40760617的博客 349

​LeetCode刷题实战276:栅栏涂色

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !...

程序IT圈 1928

Trae CN 使用入门指南

【代码】Trae CN 使用入门指南。

weixin_42551921的博客 8814

LeetCode第 276 题:栅栏涂色(C++)

276.栅栏涂色 dp[i]表示第i个栅栏的颜色方案数,根据题意,显然dp[1] = k, dp[2] = k*k。当i >= 3的时候: i和i-1颜色相同:dp[i] = dp[i-2]*(k-1),表示去掉i-2的颜色(将i-1和i连起来考虑,两者是相同的),还有k-1种可选 i和i-1颜色不同:dp[i] = dp[i-1]*(k-1),表示去掉i-1的颜色,还剩下k-1种颜色可选 求方案数,使用相加。 第一种的理解:假设颜色为1,2,3,4,如果i-2的颜色为1,因为i与i-1的颜色相

zj 968

海康工业相机BayerRG8转BGR8实战:手把手教你用OpenCV实现高效图像转换

本文详细解析了海康工业相机BayerRG8格式到BGR8格式的高效转换实战。针对工业视觉项目中高帧率、大分辨率场景下的性能瓶颈,文章对比了海康SDK原生方案与OpenCV多种转换方法的优劣,并提供了从基础实现到高级优化(如多线程流水线、内存管理、SIMD指令集应用)的完整代码示例与性能调优策略,帮助开发者构建稳定、高效的图像处理系统。

weixin_29237635的博客 527

【WSL2+Ubuntu+Docker Desktop】迁移到D盘

该篇文章包含将WSL2+ubuntu+Docker Desktop从C盘迁移到D盘

哈茶的博客 3947

LeetCode-Python-276. 栅栏涂色

有 k 种颜色的涂料和一个包含 n 个栅栏柱的栅栏,每个栅栏柱可以用其中一种颜色进行上色。 你需要给所有栅栏柱上色,并且保证其中相邻的栅栏柱 最多连续两个颜色相同。然后,返回所有有效涂色的方案数。 注意: n 和k 均为非负的整数。 示例: 输入: n = 3,k = 2 输出: 6 解析: 用 c1 表示颜色 1,c2 表示颜色 2,所有可能的涂色方案有: ...

Keep Coding 1930

CentOS7下Ollama部署全攻略:从安装到自定义端口配置(附国内加速技巧)

本文详细介绍了在CentOS7系统上部署Ollama的完整流程,包括环境准备、模型下载优化、服务化部署及端口配置等关键步骤。特别针对国内网络环境提供了加速安装方案和镜像源配置技巧,帮助开发者快速搭建稳定的本地AI运行环境。

mmm90的博客 300

LeetCode 276:栅栏涂色

题目: 状态:即栅栏的索引位置和 是否与上一个同色, 两种状态;dp定义: 指 第 i 个栅栏与第 i-1个栅栏同色的颜色可能性,指第 i 个栅栏与上一个不同色的颜色可能性选择: 根据题意,即和上一个同色,由于同一个颜色只能相邻两次,那么第 i-1个栅栏注定不能和上一个(i-2)同色! 所以 ; 而指 i 和 i-1不同色,那么 i-1位置的栅栏 既可以与i-2同色也可以与i-2不同色,且此时 i栅栏可以有 k-1种颜色可选;base case: 从 i=1 开始算,dp[1][0]=0 即第一次不可能和上

机械工程跑路哥 1117

13个GNS3 3.0.5 appliances设备模板镜像合集:IOSv/L2,IOU L2/L3,以及IOS-XE

13个GNS3 appliances设备模板:IOSv/L2,IOU L2/L3,以及IOS-XE合集

haierccc的博客 1090

微信小程序审核避坑指南:巧用 wx.getAccountInfoSync 实现审核与线上环境无感切换

本文详细介绍了如何利用`wx.getAccountInfoSync`API实现微信小程序审核与线上环境的无感切换。通过环境识别技术和动态渲染架构设计,开发者可以优雅解决审核资质门槛问题,同时保持生产环境的完整用户体验。文章包含组件化实现、全局状态管理等实用方案,帮助开发者提升审核通过率。

weixin_30527423的博客 269

leetcode276

class Solution: def wiggleMaxLength(self, nums: List[int]) -> int: if len(nums)<=1: return len(nums) curdif=0 predif=0 res=1 for i in range(len(nums)-1): curdif=nums[i+1]-nums[i] .

yys130826的博客 188

COMSOL仿真分析高压直流GIS/GIL绝缘性能优化

高压直流气体绝缘设备(GIS/GIL)的绝缘性能是保障电网可靠运行的关键。通过COMSOL Multiphysics多物理场仿真,可以深入分析气固界面电场分布与电荷积聚机理。该技术结合静电场、热场和电荷传输耦合计算,能准确模拟温度梯度对SF6气体与环氧树脂界面特性的影响。工程实践中,这类仿真可优化导体结构设计、改进绝缘材料配方,并建立电场-温度关联预警模型。特别是在80℃高温工况下,仿真显示界面电荷密度会显著增加23%,这对设备安全运行具有重要指导价值。

weixin_32075843的博客 199

【LeetCode】276. Paint Fence 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法动态规划日期 题目地址:https://leetcode-cn.com/problems/sum-of-digits-in-the-minimum-number/ 题目描述 There is a fence with n posts, ea...

负雪明烛 978

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[Leetcode] 276. Paint Fence 解题报告

题目: There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Re

魔豆(Magicbean)的博客 1016

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【dp】leetcode 276. Paint Fence

【dp】leetcode 276. Paint Fence class Solution { public int numWays(int n, int k) { if(n==0||k==0) return 0; int[][][] dp = new int[n+1][k][2]; for(int j=0;j<k;j++){

独钓寒江雪 602

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

[leetcode] 276. Paint Fence @ python

原题 There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the tot...

闲庭信步 766

Leetcode 276. Paint Fence (Easy) (cpp)

Leetcode 276. Paint Fence (Easy) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 586

[LeetCode276]Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent fence posts have the same color.Return the total num

MoM 1124

LeetCode276 Paint Fence

。。。

李歇特冯-兹拜因巴哈的博客 273

LeetCode 276. Paint Fence

#include #include using namespace std; /* There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent f

ActiveCoder的博客 388

[Leetcode]276. Paint Fence

这题虽然是easy题,但其实,我个人还是不是特别的明白。所以还是记录一下 这题其实还是有很明显的dp特征的,只是这一题的限制或者说关系并不在于n和n - 1,而是在于n和n - 2。也就是前一面fence是怎么样并不直接影响当前这一堵fence。 先探讨一下这个递推式是怎么形成的。 只有两种情况需要考虑,首先是当前的fence和之前的fence颜色不同。如果说之前的fence颜色的总数是f...

chaochen1407的专栏 715

【Leetcode】276. Paint Fence

题目地址: https://leetcode.com/problems/paint-fence/ 给定nnn个柱子,可以染kkk种颜色,要求不能有连续超过两个相邻的柱子颜色一样,问有多少种染色方法。 参考https://blog.csdn.net/qq_46105170/article/details/110361142。代码如下: public class Solution { public int numWays(int n, int k) { if (k == 0) {

数学、算法爱好者的博客 303

Leetcode 276 Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the

夜色之浓,莫过于黎明前的黑暗。 990

Leetcode 276. Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the tota...

YIQI521的博客 313

Leetcode——276.Paint Fence

DescriptionThere is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent fence posts have the same color.Return th

好记性不如烂笔头的专栏 1188
上一篇: [leetcode] 246. Strobogrammatic Number 解题报告
下一篇: [leetcode] 288. Unique Word Abbreviation 解题报告
小榕流光
博客等级 码龄16年 135粉丝 424原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值