CareerCup Arrange the 2 x 1 boards on the 2 x n board

本文探讨了如何计算使用2x1板砖完全填充2xN大小的板砖的不同方式的数量。通过将问题简化为寻找第N个斐波那契数的方法,提供了一个高效的解决方案,并进一步介绍了如何在O(log N)时间内找到该数值。
Given a board made of 2 x n squares, and boards made of 2 x 1 squares, write a function that will calculate the number of possible ways to arrange the 2 x 1 boards on the 2 x n board, in a way that will fill it completely. 

(Asked to refine the solution to be more efficient)

------------------------------------------------------------------------------------------------------

At first it seems like a dynamic programming type of problem but looking more closely it reduces to the simple problem of finding the nth Fibonacci number. Denote by f(n) the number of possibilities to tile a 2xn board. 

Consider tiling a 2xn board for some n>2. Let us sort all the possibilities according to the last 2 columns. There are f(n-1) possibilities where the last column includes a single tile and there are f(n-2) possibilities where the last 2 columns include 2 horizontally placed tiles. This covers all possibilities and hence: f(n) = f(n-1) + f(n-2) which is exactly the nth Fibonacci number. 

AND there is way to get f(n) in log(n) time. 


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值