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),不能有超过连续两根柱子是一个颜色,也就意味着第三根柱子要么根第一个柱子不是一个颜色,要么跟第二根柱子不是一个颜色。如果不是同一个颜色,计算可能性的时候就要去掉之前的颜色,也就是k-1种可能性。假设dp[1]是第一根柱子及之前涂色的可能性数量,dp[2]是第二根柱子及之前涂色的可能性数量,则dp[3]=(k-1)*dp[1] + (k-1)*dp[2]。

递推式有了,下面再讨论下base情况,所有柱子中第一根涂色的方式有k中,第二根涂色的方式则是k*k,因为第二根柱子可以和第一根一样。

State:dp[i] // 代表粉刷到第i个桩子总共有多少种刷法

Function: dp[i] = dp[i - 1] * (k - 1) + dp[i - 2] * (k - 1)

Initialize: dp[0] = k, dp[1] = k + dp[0] * (k - 1) = k * k

Return: dp[n]

Java: Time: O(n), Space: O(n)

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

  

Java: Time: O(n), Space: O(1)

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

  

[LeetCode] 276. Paint Fence 粉刷篱笆的更多相关文章

  1. [LeetCode] Paint Fence 粉刷篱笆

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

  2. [LintCode] Paint Fence 粉刷篱笆

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

  3. [LeetCode#276] Paint Fence

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

  4. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  5. 276. Paint Fence篱笆涂色

    [抄题]: There is a fence with n posts, each post can be painted with one of the k colors. You have to ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  7. 276. Paint Fence

    题目: There is a fence with n posts, each post can be painted with one of the k colors. You have to pa ...

  8. [leetcode]256. Paint House粉刷房子(三色可选)

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  9. [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

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

随机推荐

  1. opencv图片压缩视频并读取

    import os import cv2 import numpy as np import time path = './new_image/' filelist = os.listdir(path ...

  2. zookeeper 的 docker 镜像使用

    dockerhub 网址:https://hub.docker.com/_/zookeeper

  3. UVALive - 4097:Yungom(逼近 贪心)(DP)

    pro:有D个字母,每个字母有自己的权值,现状需要用它们拼出N个单词,使得这些单词互相不为另外一个的前缀. 且单词的权值和最小.D<=200; N<=200; sol:如果建立字典树,那个 ...

  4. zabbix-agent主动模式和proxy

    一.zabbix代理模式,缓解服务端压力zabbix_proxy.conf配置如下 more zabbix_proxy.conf | grep -v ^# | grep -v ^$ Server=za ...

  5. vue中点击不同的em添加class去除兄弟级class

    vue中使用v-for循环li 怎么点击每个li中的em给添加class删除兄弟元素 <html lang="en"> <head> <meta ch ...

  6. go 学习 (三):函数 & 指针 & 结构体

    一.函数 函数声明 // 声明语法 func funcName (paramName paramType, ...) (returnType, returnType...) { // do somet ...

  7. LeetCode 1004. Max Consecutive Ones III

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...

  8. S1_搭建分布式OpenStack集群_04 keystone认证服务安装配置

    一.新建数据库及用户(控制节点)# mysql -uroot -p12345678MariaDB [(none)]> CREATE DATABASE keystone;MariaDB [(non ...

  9. ROM

    ROM 是 read only memory的简称,表示只读存储器,是一种半导体存储器.只读存储器(ROM)是一种在正常工作时其存储的数据固定不变,其中的数据只能读出,不能写入,即使断电也能够保留数据 ...

  10. canvas的基本使用

    一.定义 canvas最早是由Apple引入Webkit的,<canvas>元素包含于HTML5中 HTML5的canvas元素使用JavaScript在网页上绘制图像,画布是一个矩形区域 ...