[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 ...
随机推荐
- Jenkins设置备份
安装备份插件,系统管理-插件管理 可选插件搜索backup 备份 系统管理-Backup manager 设置备份路径 恢复
- 怎么关闭win10防火墙
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /v "Disable ...
- yum install 下载后保存rpm包
keepcache=0 更改为1下载RPM包 不会自动删除 vi /etc/yum.conf [main] cachedir=/var/cache/yum/$basearch/$releasever ...
- Windows小技巧 -- 目录内打开CMD的快捷方式
工作中常常会有需要在某个文件夹内使用cmd的情况,例如运行某脚本,下面演示几种方法. 以进入以下目录操作为例: 方式一 : 常用的cd命令 cd命令是我们平常使用比较多的方式: 1. Win+R打开c ...
- Codeforces 219C - Color Stripe - [DP]
题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...
- POJ 2259 - Team Queue - [队列的邻接表]
题目链接:http://poj.org/problem?id=2259 Queues and Priority Queues are data structures which are known t ...
- HiveQL之Sort by、Distribute by、Cluster by、Order By详解
在这里解释一下select语法中的order by.sort by.distribute by.cluster by.order by语法. 一.order by语法 在hiveQL中Order by ...
- cinder 和 qt5 vs2015结合
下载编译好的cinder_0.9.1_vc2013, 用vs2015打开 cinder_0.9.1_vc2013\proj\vc2013\cinder.sln 重新编译 由于我用的qt也是vs2015 ...
- PHP之二维数组根据某个下标排序
function arraySortByElements($array2sort,$sortField,$order,$iscount=false) { $functionString=' if (' ...
- ms sql server读取xml文件存储过程-sp_xml_preparedocument
最近要在存储过程中读取xml中节点的值,然后进行sql操作: 要使用到的系统存储过程如下:sp_xml_preparedocument create procedure [dbo].[pro_Test ...