[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 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的更多相关文章
- [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 ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [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 ...
- [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 ...
- [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- EF Core 2.1变化
EF Core 2.1随.NET Core 2.1一起发布,本篇文章总结一下EF Core的新增功能,先从简单的开始说. 一.延迟加载 延迟加载不用介绍了吧,直接看一下怎样配置吧.EF Core 2. ...
- (广度搜索)A - Prime Path(11.1.1)
A - Prime Path(11.1.1) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- 在CentOS中安装arial字体
验证码不能正常显示是因为 linux 没有字体 1. widonws下载字体文件到Linux windows的字体比较多,其字体文件位于 C:\WINDOWS\Fonts . 从其中copy相应的字体 ...
- MyEclipse中同时启动两个tomcat
开发的时候,有些时候需要同时启动两个项目.首先配置tomcat,方法如下:(转载自:http://bendan123812.iteye.com/blog/1716789) 一.把Tomcat复制一份并 ...
- Int32 最大的数值是多少???(附十进制十六进制相互转换且包含正负数的java代码)
正数转二进制很简单,转十六进制也很简单. 那么负数的情况下呢?在计算机中无法识别你给的符号“+”,"-",计算机只认识0和1 那么在二进制中如何表示负数. 先简单介绍一下负数如何转 ...
- 线段树 || BZOJ 1112: [POI2008]砖块Klo
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1112 题解: 希望有连续K柱的高度是一样的,就先把1~K的数扔进线段树(线段树的下标就是数值 ...
- 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel
题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...
- css学习_css书写规范
css书写规 1.空格规范: div { color: red; } 2.选择器规范 3.属性 属性定义要另起一行 属性定义后必须以分号结尾 div { color: red; font-size: ...
- [No0000B3].NET C# 单体模式(Singleton)
单体模式(Singleton)是经常为了保证应用程序操作某一全局对象,让其保持一致而产生的对象,例如对文件的读写操作的锁定,数据库操作的时候的事务回滚,还有任务管理器操作,都是一单体模式读取的.创建一 ...
- 网关 整理 fastcgi wsgi
https://www.cnblogs.com/hzhtracy/p/4365938.html 网关协议学习:CGI.FastCGI.WSGI.uWSGI 一直对这四者的概念和区别很模糊,现在就特 ...