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.

Example:

Input: n = 3, k = 2
Output: 6
Explanation: Take c1 as color 1, c2 as color 2. All possible ways are:   post1 post2 post3
----- ----- ----- -----
1 c1 c1 c2
  2 c1 c2 c1
  3 c1 c2 c2
  4 c2 c1 c1 
5 c2 c1 c2
  6 c2 c2 c1

思路是利用两个dp, 一个same, 一个dif, same[i] 表明i 跟i-1 两个fence 的颜色是一样的, 而dif[i] 表明i 跟 i-1 两个 fence的颜色是不一样的.

init:   same[0] = same[1] = k

dif[0] = k, dif[1] = k*(k-1)

same[i] = dif[i-1]

dif[i] = (same[i-1] + dif[i-1]) *(k-1)

Code    T: O(n)   S; O(1)  using rolling array

class Solution:
def numWays(self, n, k):
if n == 0: return 0
if n == 1: return k
same, dif = [0]*2, [0]*2
same[0] = same[1] = k
dif[0], dif[1] = k, k*(k-1)
for i in range(2, n):
same[i%2] = dif[i%2-1]
dif[i%2] = (k-1)*(same[i%2-1] + dif[i%2-1])
return same[(n-1)%2] + dif[(n-1)%2]

[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 256. Paint House_Easy tag: Dynamic Programming

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

  2. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

    Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...

  4. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming

    基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...

  6. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  7. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  8. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  9. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. 拓展 NLog 优雅的输送日志到 Logstash

    在上上篇博客通过对aspnetcore启动前配置做了一些更改,以及对nlog进行了自定义字段,可以把请求记录输送到mysql,正式情况可能不会这么部署.因为近期也在学习elk,所以就打算做一个实例,结 ...

  2. Redhat7.5安装glusterfs4

    redhat7.5自带yum源不包含glusterfs4,下面通过rpm包的方式安装glusterfs4 环境查看 glusterfs官方网站下载rpm包下载地址 https://buildlogs. ...

  3. 壁虎书5 Support Vector Machine

    SVM is capable of performing linear or nonlinear classification,regression,and even outlier detectio ...

  4. C# 各类常见Exception 异常信息

    一直对报错有些迷惑的地方,什么时候try,catch那些Exception更合适,报错信息更能快速定位问题所在... 转载链接← 正文 Exception: 所有异常对象的基类. SystemExce ...

  5. .NET Core开发日志——ADO.NET与SQL Server

    在.NET世界,如果想要对数据库进行操作,总少不了ADO.NET的身影.在.NET Core里同样离不开那些熟悉的类库与API.这里简略地介绍下如何通过ADO.NET对SQL Server进行不同的处 ...

  6. RabbitMQ in Depth札记——AMQ协议

    RPC传输 作为AMQP的实现,RabbitMQ使用RPC(remote procedure call)模式进行远程会话.而不同于一般的RPC会话--客户端发出指令,服务端响应,但服务端不会向客户端发 ...

  7. linux下安装pycharm

    在 linux下打开浏览器,搜索pycharm,点击download. 下载好的文件的名称可能是 ‘pycharm-professional-2016.2.3.tar.gz’. 打开终端界面,输入命令 ...

  8. tensorflow的tile使用

    当你需要按照矩阵维度复制数据时候,可以使用tensorflow的tile函数 a1 = tf.tile(a, [2, 2]) 表示把a的第一个维度复制两次,第二个维度复制2次.注意使用tf.nn.so ...

  9. linux虚拟机设置本地yum源

    1.挂载ISO镜像 2.创建文件夹,用于挂载光盘,mkdir /mnt/cdrom mount /dev/cdrom /mnt/cdrom 3.修改 repo 文件 baseurl=file:///挂 ...

  10. 多文件上传(.net)

    找了很长时间,终于找到了: 前台: <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head ...