[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 ...
随机推荐
- ThinkPHP框架 做个简单表单 添加数据例子__ACTION__ __SELF__
public function zhuCe(){//自定义zhuCe方法和zhuCe显示表里的__ACTiON__这个相互交接 //实现两个逻辑 //1,显示注册页面 //2.向数据库添加内容 //自 ...
- 百万级数据下的mysql深度解析
首先,数据量大的时候,应尽量避免全表扫描,应考虑在 where 及 order by 涉及的列上建立索引,建索引可以大大加快数据的检索速度.但是,有些情况索引是不会起效的: 1.应尽量避免在 wher ...
- linux 下安装 redis
https://redis.io/ 1.下载 $ cd /usr/local/ $ wget http://download.redis.io/releases/redis-4.0.7.tar.gz ...
- ==和equal()的区别
“==”比较的是对象引用的地址相不相同 “equal()”比较的是内容是否相等
- 关于mysql远程连接
windows环境下简单,这里讲linux环境下的 首先,linux系统有一道防火墙 我用的是ubutun16.04LTS 要用ufw工具(命令)开启3306端口(ufw allow ) (如果安装的 ...
- 用CountDownLatch提升请求处理速度
countdownlatch是java多线程包concurrent里的一个常见工具类,通过使用它可以借助线程能力极大提升处理响应速度,且实现方式非常优雅.今天我们用一个实际案例和大家来讲解一下如何使用 ...
- omitting directory何意
使用cp命令拷贝目录时,若该目录下有子目录 需加参数 cp -r 加目标目录到路径下
- LeetCode 977 Squares of a Sorted Array 解题报告
题目要求 Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...
- gitee 使用
gitee 使用 首先在码云仓库创建对应的仓库 当你输入错误用户名和密码 需要清掉配置 git config --system --unset credential.helper 设置账号 git c ...
- mvc,EntityFramework调用分页存储过程
此文讲述mvc4+entityframework6+sqlserver2008环境下调用存储过程,实现分页. 1.分页存储过程代码如下: 分页原理用的row_number()和over()函数实现(没 ...