题目

here 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.

注意事项

n and k are non-negative integers.

样例

Given n=3, k=2 return 6

      post 1,   post 2, post 3
way1 0 0 1
way2 0 1 0
way3 0 1 1
way4 1 0 0
way5 1 0 1
way6 1 1 0
思路
动态规划问题
当我前i个posts有多少种可能性是 如果第i-1个颜色和第i个颜色不同时 s[i] = (k-1)*s[i-1];
如果第i-1个颜色和第i个颜色相同时因为不能有超过2个颜色相同,则和第i-2个颜色一定不同 s[i] = s[i-2]*(k-1);
s[i] = s[i-1]*(k-1) + s[i-2]*(k-1)
C++代码
int numWays(int n, int k) {
// Write your code here
if(n == ) return ;
if(n == ) return k;
if(n == ) return k*k;
int i;
int a[n];
a[] = k;
a[] = k*k;
for(i = ; i < n; ++i)
{
a[i] = a[i-]*(k-) + a[i-]*(k-);
}
return a[i-];
}

LintCode_514 Paint Fence的更多相关文章

  1. [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 ...

  2. [Locked] Paint Fence

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

  3. 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:二叉树下的不能相邻,求能 ...

  4. [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 ...

  5. LeetCode Paint Fence

    原题链接在这里:https://leetcode.com/problems/paint-fence/ 题目: There is a fence with n posts, each post can ...

  6. 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 ...

  7. [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 ...

  8. [Swift]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 ...

  9. 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 ...

随机推荐

  1. mysql 多表join

    两个表可以简单地写为 select a.,b. from a left join b on a.id =b.id; 三个以上 select a.,b. from a left join b on a. ...

  2. CentOS6.3搭建ZooKeeper伪集群

    1. 将zookeeper安装包移动至/home, 解压后改名为zookeeper 相关命令 # 解压 .tar.gz # 重命名 zookeeper 2. 进入zookeeper/conf/目录下, ...

  3. JavaWeb实现文件下载

    1. 编写文件上传Servlet public class FileUpload1 extends HttpServlet { @Override protected void doGet(HttpS ...

  4. JavaScrip中的循环语句

    循环语句 循环语句,也是流程控制语句中不可或缺的一种结构.在 JavaScrip中实现循环的方式有好几个一个来看 1.为什么需要循环 在具体介绍 Javascript中的循环之前,首先我们来明确一个问 ...

  5. Cesium官方教程13--Cesium和Webpack

    原文地址:https://cesiumjs.org/tutorials/cesium-and-webpack/ Cesium 和 Webpack Webpack是非常强大非常流行的JavaScript ...

  6. PAT甲级——A1094 The Largest Generation

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  7. 《DSP using MATLAB》Problem 8.21

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  8. wpf中数据绑定和INotifyPeropertyChanged的理解

    原创:转载请注明出处. 先说数据绑定: XAML代码: <Window x:Class="数据绑定和INotifyPropertyChanged.Window1" Loade ...

  9. 工业派-配置Intel神经计算棒二代(NCS2)

    最近两天在工业派ubuntu16.04上配置了Intel神经计算棒二代——Intel Neural Compute Stick,配置过程之艰辛我都不想说了,实在是太折磨人.不过历尽千辛万苦,总算让计算 ...

  10. HLog工作原理